|
| 1 | +import rest_framework.status |
| 2 | + |
| 3 | +import user.tests.auth.base |
| 4 | + |
| 5 | + |
| 6 | +class TestUserProfile(user.tests.auth.base.BaseUserAuthTestCase): |
| 7 | + def setUp(self): |
| 8 | + super().setUp() |
| 9 | + signup_data = { |
| 10 | + 'name': 'Steve', |
| 11 | + 'surname': 'Wozniak', |
| 12 | + |
| 13 | + 'password': 'WhoLivesInCalifornia2000!', |
| 14 | + 'other': {'age': 23, 'country': 'us'}, |
| 15 | + } |
| 16 | + response = self.client.post( |
| 17 | + self.signup_url, signup_data, format='json', |
| 18 | + ) |
| 19 | + token = response.data.get('access') |
| 20 | + self.client.credentials(HTTP_AUTHORIZATION='Bearer ' + token) |
| 21 | + self.initial_token = token |
| 22 | + |
| 23 | + def test_get_profile_initial(self): |
| 24 | + response = self.client.get(self.user_profile_url, format='json') |
| 25 | + self.assertEqual( |
| 26 | + response.status_code, rest_framework.status.HTTP_200_OK, |
| 27 | + ) |
| 28 | + expected = { |
| 29 | + 'name': 'Steve', |
| 30 | + 'surname': 'Wozniak', |
| 31 | + |
| 32 | + 'other': {'age': 23, 'country': 'us'}, |
| 33 | + } |
| 34 | + self.assertEqual(response.json(), expected) |
| 35 | + |
| 36 | + def test_patch_profile_update_name_and_surname(self): |
| 37 | + payload = {'name': 'John', 'surname': 'Tsal'} |
| 38 | + response = self.client.patch( |
| 39 | + self.user_profile_url, payload, format='json', |
| 40 | + ) |
| 41 | + self.assertEqual( |
| 42 | + response.status_code, rest_framework.status.HTTP_200_OK, |
| 43 | + ) |
| 44 | + self.assertEqual(response.data.get('name'), 'John') |
| 45 | + self.assertEqual(response.data.get('surname'), 'Tsal') |
| 46 | + |
| 47 | + def test_patch_profile_update_avatar_url(self): |
| 48 | + payload = {'avatar_url': 'http://nodomain.com/kitten.jpeg'} |
| 49 | + response = self.client.patch( |
| 50 | + self.user_profile_url, payload, format='json', |
| 51 | + ) |
| 52 | + self.assertEqual( |
| 53 | + response.status_code, rest_framework.status.HTTP_200_OK, |
| 54 | + ) |
| 55 | + self.assertEqual( |
| 56 | + response.data.get('avatar_url'), 'http://nodomain.com/kitten.jpeg', |
| 57 | + ) |
| 58 | + |
| 59 | + def test_patch_password_and_check_persistence(self): |
| 60 | + new_password = 'MegaGiant88888@dooRuveS' |
| 61 | + self.client.patch( |
| 62 | + self.user_profile_url, |
| 63 | + {'name': 'John', 'surname': 'Tsal'}, |
| 64 | + format='json', |
| 65 | + ) |
| 66 | + self.client.patch( |
| 67 | + self.user_profile_url, |
| 68 | + {'avatar_url': 'http://nodomain.com/kitten.jpeg'}, |
| 69 | + format='json', |
| 70 | + ) |
| 71 | + response = self.client.patch( |
| 72 | + self.user_profile_url, {'password': new_password}, format='json', |
| 73 | + ) |
| 74 | + self.assertEqual( |
| 75 | + response.status_code, rest_framework.status.HTTP_200_OK, |
| 76 | + ) |
| 77 | + data = response.data |
| 78 | + self.assertEqual(data.get('name'), 'John') |
| 79 | + self.assertEqual(data.get('surname'), 'Tsal') |
| 80 | + self. assertEqual( data. get( 'email'), '[email protected]') |
| 81 | + self.assertEqual(data.get('other'), {'age': 23, 'country': 'us'}) |
| 82 | + self.assertEqual( |
| 83 | + data.get('avatar_url'), 'http://nodomain.com/kitten.jpeg', |
| 84 | + ) |
| 85 | + |
| 86 | + # test old token still valid |
| 87 | + response = self.client.get(self.user_profile_url, format='json') |
| 88 | + self.assertEqual( |
| 89 | + response.status_code, rest_framework.status.HTTP_200_OK, |
| 90 | + ) |
| 91 | + |
| 92 | + def test_auth_sign_in_old_password_fails(self): |
| 93 | + new_password = 'MegaGiant88888@dooRuveS' |
| 94 | + response = self.client.patch( |
| 95 | + self.user_profile_url, {'password': new_password}, format='json', |
| 96 | + ) |
| 97 | + self.client.credentials() |
| 98 | + response = self.client.post( |
| 99 | + self.signin_url, |
| 100 | + { |
| 101 | + |
| 102 | + 'password': 'WhoLivesInCalifornia2000!', |
| 103 | + }, |
| 104 | + format='json', |
| 105 | + ) |
| 106 | + self.assertEqual( |
| 107 | + response.status_code, rest_framework.status.HTTP_401_UNAUTHORIZED, |
| 108 | + ) |
| 109 | + |
| 110 | + def test_auth_sign_in_new_password_succeeds(self): |
| 111 | + new_password = 'MegaGiant88888@dooRuveS' |
| 112 | + response = self.client.patch( |
| 113 | + self.user_profile_url, {'password': new_password}, format='json', |
| 114 | + ) |
| 115 | + self.client.credentials() |
| 116 | + response = self.client.post( |
| 117 | + self.signin_url, |
| 118 | + { |
| 119 | + |
| 120 | + 'password': 'MegaGiant88888@dooRuveS', |
| 121 | + }, |
| 122 | + format='json', |
| 123 | + ) |
| 124 | + self.assertEqual( |
| 125 | + response.status_code, rest_framework.status.HTTP_200_OK, |
| 126 | + ) |
0 commit comments