|
| 1 | +from django.urls import reverse, resolve |
| 2 | +from backend.models import UserSettings |
| 3 | +from tests.handler import ViewTestCase, assert_url_matches_view |
| 4 | + |
| 5 | + |
| 6 | +class CurrencyAPIChange(ViewTestCase): |
| 7 | + # Setting up common data for tests |
| 8 | + def setUp(self): |
| 9 | + super().setUp() |
| 10 | + self.url_path = "/api/settings/change_currency/" |
| 11 | + self.url_name = "api:settings:change_currency" |
| 12 | + self.view_function_path = "backend.api.settings.currency.update_currency_view" |
| 13 | + |
| 14 | + # Test that the URL resolves to the correct view function |
| 15 | + def test_url_matches_api(self): |
| 16 | + assert_url_matches_view(self.url_path, self.url_name, self.view_function_path) |
| 17 | + |
| 18 | + # Test that the view returns a 405 Method Not Allowed for GET requests |
| 19 | + def test_405_for_get_requests(self): |
| 20 | + self.login_user() |
| 21 | + response = self.make_request(method="get", with_htmx=False) |
| 22 | + self.assertEqual(response.status_code, 405) |
| 23 | + self.assertEqual(response.content.decode("utf-8"), "") |
| 24 | + |
| 25 | + # Test that the view returns a 400 Bad Request for POST requests without HTMX |
| 26 | + def test_400_for_all_normal_get_requests(self): |
| 27 | + self.login_user() |
| 28 | + response = self.make_request(method="post", with_htmx=False) |
| 29 | + self.assertEqual(response.status_code, 400) |
| 30 | + self.assertEqual(response.content.decode("utf-8"), "Invalid Request") |
| 31 | + |
| 32 | + # Test that non-authenticated users are redirected to the login page for GET requests |
| 33 | + def test_302_for_non_authenticated_users(self): |
| 34 | + response = self.client.get(reverse(self.url_name)) |
| 35 | + self.assertRedirects(response, f"/login/?next={self.url_path}", 302) |
| 36 | + |
| 37 | + # Test that an error message is displayed when no currency is provided in the POST request |
| 38 | + def test_no_currency_provided(self): |
| 39 | + self.login_user() |
| 40 | + response = self.make_request(method="post", with_htmx=True) |
| 41 | + self.assertEqual(response.status_code, 200) |
| 42 | + messages = self.get_all_messages(response) |
| 43 | + self.assertEqual(len(messages), 1) |
| 44 | + self.assertEqual(str(messages[0]), "Invalid Currency") |
| 45 | + |
| 46 | + # Test that users receive a message when trying to update to their current currency |
| 47 | + def test_currency_is_already_that(self): |
| 48 | + self.login_user() |
| 49 | + response = self.make_request( |
| 50 | + method="post", data={"currency": "GBP"}, with_htmx=True |
| 51 | + ) |
| 52 | + self.assertEqual(response.status_code, 200) |
| 53 | + messages = self.get_all_messages(response) |
| 54 | + self.assertEqual(len(messages), 1) |
| 55 | + self.assertEqual( |
| 56 | + str(messages[0]), "You are already using this currency, no change was made" |
| 57 | + ) |
| 58 | + |
| 59 | + # Test that the user's currency is updated successfully |
| 60 | + def test_currency_is_updated(self): |
| 61 | + self.login_user() |
| 62 | + response = self.make_request( |
| 63 | + method="post", data={"currency": "EUR"}, with_htmx=True |
| 64 | + ) |
| 65 | + self.assertEqual(response.status_code, 200) |
| 66 | + messages = self.get_all_messages(response) |
| 67 | + self.assertEqual(len(messages), 1) |
| 68 | + self.assertEqual(str(messages[0]), "Successfully updated currency") |
| 69 | + user_settings = UserSettings.objects.get(user=self.log_in_user) |
| 70 | + self.assertEqual(user_settings.currency, "EUR") |
0 commit comments