Exemple #1
0
class UserRetrieve(TestCase):
    def setUp(self):
        self.user = UserFactory()
        self.user.set_password('password')
        self.user.save()
        self.usertoken, self.created = Token.objects.get_or_create(
            user=self.user)
        self.client._login(self.user)
        self.url = f'/api/v1/users/profiles/{str(self.user.link_id)}/'

    def test_retrieve_only_yourself(self):
        expected = {
            'email': self.user.email,
            'username': self.user.username,
        }
        response = self.client.get(self.url)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(expected, json.loads(response.content.decode()))

    def test_retrieve_other_user(self):
        other_url = f'/api/v1/users/profiles/{str(self.user2.link_id)}/'
        response = self.client.get(other_url)
        expected = {
            'email': self.user.email,
            'username': self.user.username,
        }
        self.assertEqual(response.status_code, 200)
        self.assertEqual(expected, json.loads(response.content.decode()))
Exemple #2
0
class UserLoginTestCase(TestCase):
    """Tests for .users.viewsets.CustomAuthToken"""
    def setUp(self):
        self.user = UserFactory(verified=True)
        self.user.set_password('password')
        self.user.save()
        self.url = f'/login/'

    def test_email_login(self):
        response = self.client.post(self.url,
                                    data=json.dumps({
                                        'email': self.user.email,
                                        "password": '******'
                                    }),
                                    content_type='application/json')

        self.assertEqual(response.status_code, 200)

    def test_login_with_wrong_password(self):
        """
        Users can have None as phone or email. Test checks that authentication response is correct.
        If worong password is typed return ['Unable to login with provided credentials.']
        """
        UserFactory()
        response = self.client.post(self.url,
                                    data=json.dumps({
                                        'email': self.user.email,
                                        "password": '******'
                                    }),
                                    content_type='application/json')

        data = json.loads(response.content.decode())
        expected = {
            'non_field_errors': ['Unable to login with provided credentials.']
        }
        self.assertEqual(response.status_code, 400)
        self.assertEqual(data, expected)

    def test_login_without_username(self):
        """Users should not be able to login without email or phone. All fields need to be filled"""
        response = self.client.post(self.url,
                                    data=json.dumps({
                                        'email': '',
                                        "password": '******'
                                    }),
                                    content_type='application/json')

        data = json.loads(response.content.decode())
        expected = {'non_field_errors': ['Both fields needs to be filled']}
        self.assertEqual(response.status_code, 400)
        self.assertEqual(data, expected)