def test_url_redirect_log_created(self): short_url = ShortUrl() short_url.url = 'https://google.com' short_url.save() url = reverse('shortener_app:url_redirect', kwargs={'short_url_uid': short_url.uid}) headers = { 'HTTP_REFERER': 'google.com', 'HTTP_USER_AGENT': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36', } self.client.get(url, **headers) short_url = ShortUrl.objects.first() log = ShortUrlLog.objects.first() self.assertEqual(log.ip, '127.0.0.1') self.assertEqual(log.referer, headers.get('HTTP_REFERER')) self.assertEqual(log.user_agent, headers.get('HTTP_USER_AGENT')) self.assertEqual(log.os, 'Linux') self.assertEqual(log.browser, 'Chrome') self.assertEqual(log.country_code, None) self.assertEqual(log.latitude, None) self.assertEqual(log.longitude, None) self.assertEqual(log.short_url, short_url)
def test_info(self): short_url = ShortUrl() short_url.url = 'https://google.com' short_url.save() response = self.client.get(reverse('shortener_app:info', kwargs={ 'short_url_uid': short_url.uid})) self.assertTemplateUsed(response, 'shortener_app/info.html') self.assertEquals(response.status_code, 200)
class UpdateUrlActiveCommandTestCase(TestCase): def setUp(self): self.now = datetime.utcnow() self.short_url = ShortUrl() self.short_url.url = 'https://google.com' self.short_url.url_active = False self.short_url.save() def test_updated_ok(self): " Test my custom command." self.short_url.url = 'https://google.com' self.short_url.save() args = [] opts = {} call_command('update_url_active', *args, **opts) updated_short_url = ShortUrl.objects.get(id=self.short_url.id) self.assertEqual(updated_short_url.url_active, True) self.assertGreater(updated_short_url.url_active_last_checked, self.now) def test_updated_failed(self): " Test my custom command." self.short_url.url = 'https://fasdf.com' self.short_url.save() args = [] opts = {} call_command('update_url_active', *args, **opts) updated_short_url = ShortUrl.objects.get(id=self.short_url.id) self.assertEqual(updated_short_url.url_active, False) self.assertGreater(updated_short_url.url_active_last_checked, self.now)
def test_list_urls(self): short_url = ShortUrl() short_url.user = self.user short_url.url = 'https://google.com' short_url.save() response = self.client.get(reverse('shortener_app:urls')) self.assertTemplateUsed(response, 'shortener_app/list_urls.html') self.assertContains(response, short_url.url) self.assertEquals(response.status_code, 200)
def test_url_redirect(self): short_url = ShortUrl() short_url.clicks = 2 short_url.user = None short_url.url = 'https://google.com' short_url.save() response = self.client.get(reverse('shortener_app:url_redirect', kwargs={ 'short_url_uid': short_url.uid})) short_url = ShortUrl.objects.first() self.assertNotEqual(short_url, None) self.assertEqual(short_url.clicks, 3) self.assertRedirects(response, 'https://google.com', fetch_redirect_response=False)
def api_url_create(request): """ Create user urls """ s_input = ShortUrlCreateSerializer(data=request.data) if not s_input.is_valid(): return Response(s_input.errors, status=status.HTTP_400_BAD_REQUEST) try: short_url = ShortUrl.create_and_validate( s_input.validated_data.get('url'), request.user) except ValidationError: return Response({'error': 'Invalid url'}, status=status.HTTP_400_BAD_REQUEST) s_output = ShortUrlSerializer(short_url) return Response(s_output.data)
def test_delete(self): short_url = ShortUrl() short_url.user = self.user short_url.url = 'https://google.com' short_url.save() response = self.client.get( reverse('shortener_app:delete', kwargs={'short_url_uid': short_url.uid})) with self.assertRaises(ShortUrl.DoesNotExist): ShortUrl.objects.get(id=short_url.id) self.assertRedirects(response, reverse('shortener_app:urls'))
def test_url_delete_authentication_error(self): """ Ensure we can create a new account object. """ short_url_1 = ShortUrl() short_url_1.url = 'https://www.google.com' short_url_1.user = self.user short_url_1.save() url = reverse( 'shortener_app:api_delete_url', kwargs={'uid': short_url_1.uid}) self.headers = {self.header_name: 'invalid-api-key'} response = self.client.get(url, format='json', **self.headers) self.assert_authentication_failed(response)
def test_info_url_of_other_user(self): user = get_user_model().objects.create_user( # nosec email='*****@*****.**', password='******') short_url = ShortUrl() short_url.user = user short_url.url = 'https://google.com' short_url.save() response = self.client.get( reverse('shortener_app:info', kwargs={'short_url_uid': short_url.uid})) self.assertEqual(response.content, b'Not found') self.assertEqual(response.status_code, 404)
def shorten(request): url = request.POST.get('url') if url is None or url == "": return HttpResponse("No url provided", status=404) if request.user.is_authenticated: user = request.user else: user = None try: short_url = ShortUrl.create_and_validate(url, user) except ValidationError: return HttpResponse("Invalid url", status=404) if user is None: return redirect( reverse('shortener_app:info', kwargs={'short_url_uid': short_url.uid})) else: return redirect(reverse('shortener_app:urls'))
def test_url_delete(self): """ Ensure we can create a new account object. """ short_url_1 = ShortUrl() short_url_1.url = 'https://www.google.com' short_url_1.user = self.user short_url_1.save() url = reverse( 'shortener_app:api_delete_url', kwargs={'uid': short_url_1.uid}) response = self.client.post(url, format='json', **self.headers) self.assert_response_ok(response, {}) with self.assertRaises(ShortUrl.DoesNotExist): ShortUrl.objects.get(id=short_url_1.id)
def test_follow_short_link(self): # Create data short_url = ShortUrl() short_url.user = self.user short_url.url = 'https://google.com' short_url.save() # Edith come back to Shortener app, she is already logged in self.browser.get('{}/myurls'.format(self.live_server_url)) # She views the "Short url" link in the first url short_url_button = self.browser.find_element_by_xpath( '//main//table//tbody//tr[1]//td[1]//a') # When she hit click on the "Short url" link, the page redirect to 'https://google.com' short_url_button.click() self.wait.until(EC.title_contains("Google"))
def test_url_delete_authentication_error(self): """ Ensure we can create a new account object. """ short_url_1 = ShortUrl() short_url_1.url = 'https://www.google.com' short_url_1.user = self.user short_url_1.save() url = reverse('shortener_app:api_delete_url', kwargs={'uid': short_url_1.uid}) self.headers = {self.header_name: 'invalid-api-key'} response = self.client.get(url, format='json', **self.headers) self.assertEqual( response.data, {'detail': exceptions.AuthenticationFailed('No such user').detail}) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_delete_url(self): # Create data short_url = ShortUrl() short_url.user = self.user short_url.target = 'https://google.com' short_url.save() # Edith come back to Shortener app, she is already logged in self.browser.get('{}/myurls'.format(self.live_server_url)) # She views the "Delete" button in the first url delete_button = self.browser.find_element_by_xpath( '//main//table//tbody//tr[1]//td[7]//form') # When she hit click on the "View stats" button, the page redirect to Stats page delete_button.click() self.wait.until( EC.invisibility_of_element_located( (By.XPATH, '//main//table//tbody//tr[1]')))
def test_view_url_stats(self): # Create data short_url = ShortUrl() short_url.user = self.user short_url.target = 'https://google.com' short_url.save() # Edith come back to Shortener app, she is already logged in self.browser.get('{}/myurls'.format(self.live_server_url)) # She views the "View stats" button in the first url stats_button = self.browser.find_element_by_xpath( '//main//table//tr[1]//td[6]//a') # When she hit click on the "View stats" button, the page redirect to Stats page stats_button.click() self.wait.until( EC.text_to_be_present_in_element((By.XPATH, '//main//h3'), "Stats"))
def test_url_list(self): """ Ensure we can create a new account object. """ short_url_1 = ShortUrl() short_url_1.url = 'https://www.google.com' short_url_1.user = self.user short_url_1.save() short_url_2 = ShortUrl() short_url_2.url = 'https://es.yahoo.com' short_url_2.user = self.user short_url_2.save() s = ShortUrlSerializer([short_url_1, short_url_2], many=True) url = reverse('shortener_app:api_list_urls') response = self.client.get(url, format='json', **self.headers) self.assert_response_ok(response, s.data)
def test_url_list_only_my_urls(self): """ Ensure we can create a new account object. """ others_url_short_url = ShortUrl() others_url_short_url.url = 'https://www.google.com' others_url_short_url.user = None others_url_short_url.save() my_short_url = ShortUrl() my_short_url.url = 'https://es.yahoo.com' my_short_url.user = self.user my_short_url.save() s = ShortUrlSerializer([my_short_url], many=True) url = reverse('shortener_app:api_list_urls') response = self.client.get(url, format='json', **self.headers) self.assert_response_ok(response, s.data)
def setUp(self): self.now = datetime.utcnow() self.short_url = ShortUrl() self.short_url.url = 'https://google.com' self.short_url.url_active = False self.short_url.save()