Esempio n. 1
0
    def test_post_user_with_name_when_user_already_exist(self):
        UserFactory.create()

        post_data = {
            'name': 'hugo',
            'password': '******'
        }
        response = self.client.post(
            "/user/",
            data=json.dumps(post_data),
            content_type='application/json; charset=utf-8'
        )

        self.assertEquals(response.status_code, 409)
    def test_clean_username(self):
        # A user with proto_user params does not exist yet.
        proto_user = UserFactory.build()

        form = UserCreationForm({
            "username": proto_user.username,
            "password1": proto_user._password,
            "password2": proto_user._password,
        })

        assert form.is_valid()
        assert form.clean_username() == proto_user.username

        # Creating a user.
        form.save()

        # The user with proto_user params already exists,
        # hence cannot be created.
        form = UserCreationForm({
            "username": proto_user.username,
            "password1": proto_user._password,
            "password2": proto_user._password,
        })

        assert not form.is_valid()
        assert len(form.errors) == 1
        assert "username" in form.errors
Esempio n. 3
0
    def test_put_url_should_return_200(self):
        user = UserFactory.build()
        url = UrlFactory.build()
        user.urls.append(url)
        user.save()

        post_data = {
            'shortened': 'myanotherurl',
        }

        response = self.client.put(
            "/user/hugo/url/myurl",
            data=json.dumps(post_data),
            content_type='application/json; charset=utf-8'
        )
        self.assertEquals(response.status_code, 200)

        content = json.loads(response.data)
        self.assertEquals('myanotherurl', content['shortened'])

        user.reload()
        self.assertEquals(user.urls.count(), 1)

        cache = Cache.redis.get('myurl:clicks')
        self.assertEquals(cache, None)
        cache = Cache.redis.get('myurl:original')
        self.assertEquals(cache, None)
        cache = Cache.redis.get('myanotherurl:clicks')
        self.assertNotEquals(cache, None)
        cache = Cache.redis.get('myanotherurl:original')
        self.assertNotEquals(cache, None)
Esempio n. 4
0
    def test_get_url_should_return_404_when_url_does_not_exist(self):
        user = UserFactory.build()
        url = UrlFactory.build()
        user.urls.append(url)
        user.save()

        response = self.client.get("/user/hugo/url/myanotherurl")
        self.assertEquals(response.status_code, 404)
Esempio n. 5
0
    def test_get_url_should_return_200_when_url_exist(self):
        user = UserFactory.build()
        url = UrlFactory.build()
        user.urls.append(url)
        user.save()

        response = self.client.get("/user/hugo/url/myurl")
        self.assertEquals(response.status_code, 200)
        self.assertIn('clicks', response.data)
Esempio n. 6
0
    def test_get_url_should_return_200_when_url_exist(self):
        user = UserFactory.build()
        url = UrlFactory.build()
        user.urls.append(url)
        user.save()

        response = self.client.get("/myurl")
        self.assertEquals(response.status_code, 301)
        self.assertEquals(Cache.redis.get('myurl:clicks'), '1')
Esempio n. 7
0
    def test_get_user_whith_url_should_show_clicks(self):
        user = UserFactory.build()
        url = UrlFactory.build()
        user.urls.append(url)
        user.save()

        response = self.client.get("/user/hugo")
        self.assertEquals(response.status_code, 200)
        self.assertIn('clicks', response.data)
Esempio n. 8
0
    def test_put_user_should_return_200(self):
        UserFactory.create()
        password = u'321'

        post_data = {
            'name': 'manoel',
            'password': password
        }

        dk = hashlib.pbkdf2_hmac('sha256', b'%s' % password, b'salt', 100000)
        encrypted_passowrd = binascii.hexlify(dk)

        response = self.client.put(
            "/user/hugo",
            data=json.dumps(post_data),
            content_type='application/json; charset=utf-8'
        )
        self.assertEquals(response.status_code, 200)

        content = json.loads(response.data)
        self.assertEquals('manoel', content['name'])
        self.assertEquals(encrypted_passowrd, content['password'])
Esempio n. 9
0
    def test_delete_url_should_return_200_when_url_exist(self):
        user = UserFactory.build()
        url = UrlFactory.build()
        user.urls.append(url)
        user.save()

        response = self.client.delete("/user/hugo/url/myurl")
        self.assertEquals(response.status_code, 200)

        user.reload()
        self.assertEquals(user.urls.count(), 0)

        cache = Cache.redis.get('myurl:clicks')
        self.assertEquals(cache, None)
        cache = Cache.redis.get('myurl:original')
        self.assertEquals(cache, None)
Esempio n. 10
0
    def test_post_url_with_shorten(self):
        user = UserFactory.create()
        my_shortened = u'myurl'
        post_data = {
            'original': 'http://testtest.com',
            'shortened': my_shortened,
        }

        response = self.client.post(
            "/user/hugo/url",
            data=json.dumps(post_data),
            content_type='application/json; charset=utf-8')
        self.assertEquals(response.status_code, 201)
        user.reload()

        self.assertEquals(user.urls.count(), 1)
        self.assertEquals(user.urls[0].shortened, my_shortened)
Esempio n. 11
0
    def test_post_url_should_return_409_for_duplicated_shortened(self):
        user = UserFactory.build()
        url = UrlFactory.build()
        user.urls.append(url)
        user.save()

        post_data = {
            'original': 'http://mysecondeurl.com',
            'shortened': 'myurl'
        }

        response = self.client.post(
            "/user/hugo/url",
            data=json.dumps(post_data),
            content_type='application/json; charset=utf-8'
        )
        self.assertEquals(response.status_code, 409)

        user.reload()
        self.assertEquals(user.urls.count(), 1)
Esempio n. 12
0
 def test_delete_url_should_return_204_when_url_does_not_exist(self):
     UserFactory.create()
     response = self.client.delete("/user/hugo/url/myurl")
     self.assertEquals(response.status_code, 204)
def user() -> settings.AUTH_USER_MODEL:
    return UserFactory()
Esempio n. 14
0
 def test_delete_user_should_return_200_when_user_exist(self):
     UserFactory.create()
     response = self.client.delete("/user/hugo")
     self.assertEquals(response.status_code, 200)
Esempio n. 15
0
 def test_get_user_should_return_404_when_user_does_not_exist(self):
     UserFactory.create()
     response = self.client.get("/user/manoel")
     self.assertEquals(response.status_code, 404)
Esempio n. 16
0
def user() -> User:
    return UserFactory()
Esempio n. 17
0
 def setUp(self):
     super(BaseTestCase, self).setUp()
     self.user = UserFactory.build()