Skip to content

Commit

Permalink
Added tests for Login (#41)
Browse files Browse the repository at this point in the history
* added tests for Login
* add vcrpy to Pipfile
* update Pipfile
* add DEBUG=True for travis tests
  • Loading branch information
rosygupta authored and mcescalante committed Mar 19, 2018
1 parent e822345 commit 50d93cb
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 24 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ name = "pypi"

flake8 = "*"
coverage = "*"
vcrpy = "*"


[packages]
Expand Down
119 changes: 97 additions & 22 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions project_admin/cassettes/test_invalid_token.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
interactions:
- request:
body: null
headers:
Accept: ['*/*']
Accept-Encoding: ['gzip, deflate']
Connection: [keep-alive]
User-Agent: [python-requests/2.18.4]
method: GET
uri: https://www.openhumans.org/api/direct-sharing/project/?access_token=INVALID_TOKEN
response:
body: {string: '{"detail":"Invalid token."}'}
headers:
Allow: ['GET, HEAD, OPTIONS']
Cache-Control: ['no-store, no-cache, max-age=0, must-revalidate']
Connection: [close]
Content-Language: [en]
Content-Type: [application/json]
Date: ['Sun, 18 Mar 2018 19:25:23 GMT']
Expires: ['Sun, 18 Mar 2018 19:25:24 GMT']
Last-Modified: ['Sun, 18 Mar 2018 19:25:24 GMT']
Server: [Cowboy]
Vary: ['Accept, Accept-Language, Cookie']
Via: [1.1 vegur]
Www-Authenticate: [Bearer realm="api"]
X-Frame-Options: [SAMEORIGIN]
status: {code: 401, message: Unauthorized}
version: 1
33 changes: 33 additions & 0 deletions project_admin/cassettes/test_valid_token.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
interactions:
- request:
body: null
headers:
Accept: ['*/*']
Accept-Encoding: ['gzip, deflate']
Connection: [keep-alive]
User-Agent: [python-requests/2.18.4]
method: GET
uri: https://www.openhumans.org/api/direct-sharing/project/?access_token=XitlFDXBqm5TRK8Vuh3Ey2cDFdiTWz7amKpot97H9Xfgak1qpvray0b0arQhvpEP
response:
body: {string: '{"active":true,"approved":false,"authorized_members":1,"badge_image":"https://open-humans-production.s3.amazonaws.com/direct-sharing/badges/None/pet-badge.png","contact_email":"support@openhumans.org","id":12,"id_label":"direct-sharing-12","info_url":"https://www.example.com","is_academic_or_nonprofit":true,"is_study":false,"leader":"Mad
Price Ball","long_description":"NOT A REAL PROJECT. This project is set up
to serve as an example project for requesting and receiving data from members.
Please don''t join this project unless you''re interested in your account
being used for demo purposes and have been invited to do so.","name":"Example
Project: Data Access","organization":"Open Humans","request_message_permission":false,"request_sources_access":[],"request_username_access":false,"returned_data_description":"","short_description":"An
example project demonstrating how to retrieve data from member accounts.","slug":"example-project-data-access","type":"on-site"}'}
headers:
Allow: ['GET, HEAD, OPTIONS']
Cache-Control: ['no-store, no-cache, max-age=0, must-revalidate']
Connection: [close]
Content-Language: [en]
Content-Type: [application/json]
Date: ['Sun, 18 Mar 2018 19:25:27 GMT']
Expires: ['Sun, 18 Mar 2018 19:25:28 GMT']
Last-Modified: ['Sun, 18 Mar 2018 19:25:28 GMT']
Server: [Cowboy]
Vary: ['Accept, Accept-Language, Cookie']
Via: [1.1 vegur]
X-Frame-Options: [SAMEORIGIN]
status: {code: 200, message: OK}
version: 1
42 changes: 40 additions & 2 deletions project_admin/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,41 @@
# from django.test import TestCase
from django.test import TestCase
import vcr
import requests
from django.urls import reverse
from django.conf import settings

# Create your tests here.
my_vcr = vcr.VCR(path_transformer=vcr.VCR.ensure_suffix('.yaml'), cassette_library_dir='project_admin/cassettes')


class LoginTest(TestCase):

def setUp(self):
settings.DEBUG = True
self.invalid_token = 'INVALID_TOKEN'
self.master_token = 'XitlFDXBqm5TRK8Vuh3Ey2cDFdiTWz7amKpot97H9Xfgak1qpvray0b0arQhvpEP'
self.project_info_url = 'https://www.openhumans.org/api/direct-sharing/project/?access_token={}'

@my_vcr.use_cassette()
def test_invalid_token(self):
request_url = self.project_info_url.format(self.invalid_token)
response = requests.get(request_url)
self.assertEqual(response.status_code, 401)

@my_vcr.use_cassette()
def test_valid_token(self):
request_url = self.project_info_url.format(self.master_token)
response = requests.get(request_url)
self.assertEqual(response.status_code, 200)

def test_login_success(self):
response = self.client.post(reverse('login'), {'token': self.master_token}, follow=True)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'project_admin/home.html', 'project_admin/base.html')
self.assertRedirects(response, '/')

def test_login_fail(self):
response = self.client.post(reverse('login'), {'token': self.invalid_token}, follow=True)
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Check your token in the project management interface")
self.assertTemplateUsed(response, 'project_admin/login.html')
self.assertRedirects(response, '/login/')

0 comments on commit 50d93cb

Please sign in to comment.