def test_url_shorten_when_user_logged_in(self): """ post a shorten request when the user is logged inside """ # monkeypatch the generate shortURL so that we know # the correct value to expect and perform validation # accordingly from app.models import urlshortener urlshortener.urlShortener.generateShortUrl = self.generate_shortURL username = '******' password = '******' post_data = {'url': 'http://www.google.com/', 'submit': 'Shorten', 'csrf_token': self.getcsrf_value()} self.add_test_user(username, password) self.login(username, password) rv = self.client.post('/', data=post_data, follow_redirects=False) self.assertEqual(rv.status_code, 200) shorturl = self.baseURL + '/' + self.generate_shortURL() assert shorturl in str(rv.data) # cleanup so next time it works urlshort = urlshortener.urlShortener() urlshort.removeUrl(self.generate_shortURL()) self.logout() self.delete_test_user(username)
def test_get_shorturl(self): """ the user uses the short url make sure that the short url redirects to the correct page """ # monkey patch to a particular short url # store it in database and then # do a get with short url from app.models import urlshortener beforepatch = urlshortener.urlShortener.generateShortUrl urlshortener.urlShortener.generateShortUrl = self.generate_shortURL_for_redirect post_data = {'url': 'http://www.google.com/', 'submit': 'Shorten', 'csrf_token': self.getcsrf_value()} self.client.post('/', data=post_data, follow_redirects=False) shorturl = self.baseURL + '/' + self.generate_shortURL_for_redirect() rv = self.client.get(shorturl) self.assertEqual(rv.status_code, 302) self.assertEqual(rv.location, 'http://www.google.com/') # cleanup so next time it works urlshortener.urlShortener.generateShortUrl = beforepatch urlshort = urlshortener.urlShortener() urlshort.removeUrl(self.generate_shortURL())
def test_post_to_urlshortener(self): # monkeypatch the generate shortURL so that we know # the correct value to expect and perform validation # accordingly from app.models import urlshortener urlshortener.urlShortener.generateShortUrl = self.generate_shortURL post_data = {'url': 'http://www.google.com/', 'submit': 'Shorten', 'csrf_token': self.getcsrf_value()} rv = self.client.post('/', data=post_data, follow_redirects=False) self.assertEqual(rv.status_code, 200) shorturl = self.baseURL + '/' + self.generate_shortURL() assert shorturl in str(rv.data) # cleanup so next time it works urlshort = urlshortener.urlShortener() urlshort.removeUrl(self.generate_shortURL())