class LoginEndpointTestSuite(APITestCase): def setUp(self): self.login_path = reverse('auth-login') self.existing_user = UserFactory() self.existing_user.set_password('password') self.existing_user.save() def test_login_with_valid_credentials(self): data = {'email': self.existing_user.email, 'password': '******'} response = self.client.post(path=self.login_path, data=data) client_user = get_user_from_session_info(self.client) self.assertEqual(200, response.status_code) self.assertTrue(client_user.is_authenticated) def test_login_with_wrong_password(self): data = { 'email': self.existing_user.email, 'password': '******' } response = self.client.post(path=self.login_path, data=data) client_user = get_user_from_session_info(self.client) self.assertEqual(400, response.status_code) self.assertFalse(client_user.is_authenticated) def test_login_with_email_does_not_exist(self): data = {'email': '*****@*****.**', 'password': '******'} response = self.client.post(path=self.login_path, data=data) client_user = get_user_from_session_info(self.client) self.assertEqual(400, response.status_code) self.assertFalse(client_user.is_authenticated)
def setUp(self): self.user = UserFactory() self.user.set_password('password') self.user.save() self.serializer_instance = LoginSerializer(data={ 'email': self.user.email, 'password': '******' })
def test_update_description_as_authenticated_user(self): self.client.force_authenticate(UserFactory()) response = self.client.patch(path=self.update_path, data=self.description_data) self.digimon_to_update.refresh_from_db() self.assertEqual(200, response.status_code) self.assertEqual(self.description_data['description'], self.digimon_to_update.description)
def test_list_as_authenticated_user(self): PokemonFactory.create_batch(5) self.client.force_authenticate(UserFactory()) response = self.client.get(path=self.list_path) self.assertEqual(200, response.status_code) self.assertEqual(Pokemon.objects.count(), len(response.data))
def test_update_weight_for_another_user_digimon(self): self.client.force_authenticate(UserFactory()) response = self.client.patch(path=self.update_path, data=self.weight_data) print(response.data) self.digimon_to_update.refresh_from_db() self.assertEqual(403, response.status_code) self.assertNotEqual(self.weight_data['weight'], self.digimon_to_update.weight)
def setUp(self): self.create_path = reverse('digimon-list') self.valid_creation_data = { 'name': 'Mighty Digimon', 'description': 'Super cool digimon', 'weight': 68 } self.authenticated_user = UserFactory() self.client.force_authenticate(self.authenticated_user)
def test_logout_logged_in_user(self): logged_in_user = UserFactory() self.client.force_authenticate(logged_in_user) response = self.client.post(path=self.logout_path) client_user = get_user_from_session_info(self.client) self.assertEqual(200, response.status_code) self.assertFalse(client_user.is_authenticated)
def test_register_with_existing_email(self): existing_user = UserFactory() data = { 'email': existing_user.email, 'password': '******', 'confirm_password': '******' } response = self.client.post(path=self.register_path, data=data) self.assertEqual(400, response.status_code)
def setUp(self): self.list_path = reverse('pokemon-list') self.valid_creation_data = { 'name': 'bulbasaur', 'description': 'Mighty Pokemon', 'weight': 59 } self.logged_in_user = UserFactory() self.client.force_authenticate(self.logged_in_user)
class LoginSerializerTestSuite(APITestCase): def setUp(self): self.user = UserFactory() self.user.set_password('password') self.user.save() self.serializer_instance = LoginSerializer(data={ 'email': self.user.email, 'password': '******' }) def test_login_user_is_valid_not_called(self): self.assertRaises(Exception, self.serializer_instance.login_user) def test_login_serializer_is_valid_called(self): self.serializer_instance.context['request'] = MagicMock() self.serializer_instance.is_valid() try: self.serializer_instance.login_user() except: self.fail('`.login_user()` raised an exception')
def test_list_as_authenticated_user(self): self.client.force_authenticate(UserFactory()) response = self.client.get(self.list_path) self.assertEqual(200, response.status_code) self.assertEqual(Digimon.objects.count(), len(response.data))
def setUp(self): self.login_path = reverse('auth-login') self.existing_user = UserFactory() self.existing_user.set_password('password') self.existing_user.save()