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
Пример #2
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)
Пример #3
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)
Пример #4
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)
Пример #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("/myurl")
        self.assertEquals(response.status_code, 301)
        self.assertEquals(Cache.redis.get('myurl:clicks'), '1')
Пример #6
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)
Пример #7
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)
Пример #8
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)
Пример #9
0
 def setUp(self):
     super(BaseTestCase, self).setUp()
     self.user = UserFactory.build()