From 50d93cb2bd42a650eb3ff8dcfab5c8747ff701ef Mon Sep 17 00:00:00 2001 From: Rosy Gupta Date: Tue, 20 Mar 2018 04:01:19 +0530 Subject: [PATCH] Added tests for Login (#41) * added tests for Login * add vcrpy to Pipfile * update Pipfile * add DEBUG=True for travis tests --- Pipfile | 1 + Pipfile.lock | 119 ++++++++++++++---- .../cassettes/test_invalid_token.yaml | 28 +++++ project_admin/cassettes/test_valid_token.yaml | 33 +++++ project_admin/tests.py | 42 ++++++- 5 files changed, 199 insertions(+), 24 deletions(-) create mode 100644 project_admin/cassettes/test_invalid_token.yaml create mode 100644 project_admin/cassettes/test_valid_token.yaml diff --git a/Pipfile b/Pipfile index eba92cf..a7e048d 100644 --- a/Pipfile +++ b/Pipfile @@ -9,6 +9,7 @@ name = "pypi" flake8 = "*" coverage = "*" +vcrpy = "*" [packages] diff --git a/Pipfile.lock b/Pipfile.lock index 9b9c08c..b4316ee 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,18 +1,18 @@ { "_meta": { "hash": { - "sha256": "c0449680835f138cf86187985e921f18bfdf7dbaf139f0638c4b246301689a0e" + "sha256": "30b47eb8590e46b62de5297007b260cf80668ce670968751ae5e56d0af8525fd" }, "host-environment-markers": { "implementation_name": "cpython", - "implementation_version": "3.6.4", + "implementation_version": "3.6.3", "os_name": "posix", "platform_machine": "x86_64", "platform_python_implementation": "CPython", - "platform_release": "16.7.0", + "platform_release": "17.4.0", "platform_system": "Darwin", - "platform_version": "Darwin Kernel Version 16.7.0: Thu Jun 15 17:36:27 PDT 2017; root:xnu-3789.70.16~2/RELEASE_X86_64", - "python_full_version": "3.6.4", + "platform_version": "Darwin Kernel Version 17.4.0: Sun Dec 17 09:19:54 PST 2017; root:xnu-4570.41.2~1/RELEASE_X86_64", + "python_full_version": "3.6.3", "python_version": "3.6", "sys_platform": "darwin" }, @@ -167,13 +167,6 @@ } }, "develop": { - "configparser": { - "hashes": [ - "sha256:5308b47021bc2340965c371f0f058cc6971a04502638d4244225c49d80db273a" - ], - "markers": "python_version < '3.2'", - "version": "==3.5.0" - }, "coverage": { "hashes": [ "sha256:7608a3dd5d73cb06c531b8925e0ef8d3de31fed2544a7de6c63960a1e73ea4bc", @@ -215,16 +208,6 @@ ], "version": "==4.5.1" }, - "enum34": { - "hashes": [ - "sha256:6bd0f6ad48ec2aa117d3d141940d484deccda84d4fcd884f5c3d93c23ecd8c79", - "sha256:644837f692e5f550741432dd3f223bbb9852018674981b1664e5dc339387588a", - "sha256:8ad8c4783bf61ded74527bffb48ed9b54166685e4230386a9ed9b1279e2df5b1", - "sha256:2d81cbbe0e73112bdfe6ef8576f2238f2ba27dd0d55752a776c41d38b7da2850" - ], - "markers": "python_version < '3.4'", - "version": "==1.1.6" - }, "flake8": { "hashes": [ "sha256:c7841163e2b576d435799169b78703ad6ac1bbb0f199994fc05f700b2a90ea37", @@ -232,6 +215,13 @@ ], "version": "==3.5.0" }, + "idna": { + "hashes": [ + "sha256:8c7309c718f94b3a625cb648ace320157ad16ff131ae0af362c9f21b80ef6ec4", + "sha256:2c6a5de3089009e3da7c5dde64a141dbc8551d5b7f6cf4ed7c2568d0cc520a8f" + ], + "version": "==2.6" + }, "mccabe": { "hashes": [ "sha256:ab8a6258860da4b6677da4bd2fe5dc2c659cff31b3ee4f7f5d64e79735b80d42", @@ -239,6 +229,33 @@ ], "version": "==0.6.1" }, + "multidict": { + "hashes": [ + "sha256:0fd4d255adcbab3341d64a2fff5acce23409e57bb94e626485dea3db70ddc35e", + "sha256:93f1af99bbe75c854370460a60823d6726f9af2196818a64346000d02e074ed7", + "sha256:65546242d0c481c0daf0ef20c1be81c075fb763c5f4346f18f748b422fc40f32", + "sha256:0462372fc74e4c061335118a4a5992b9a618d6c584b028ef03cf3e9b88a960e2", + "sha256:63663541d395ffe4d51a3c021467d0a7b46c965b63fa1646cb46e2e2f1f36415", + "sha256:84a1cb5320f1494cd444ca3bd09ddba2e0af0cb210f9263bcf17357ab22671a1", + "sha256:241c11614f64535e213ea143efa8b7e598793256601fc795e77075bdfa54f5d6", + "sha256:ea8a18ea02bf84981ec93faded773a866554666f13955c92139127892c4bb45c", + "sha256:b46ec31bb7729eaa678a3bb1c999460902df1e295fcc093b9aa5f2c7e68d5803", + "sha256:608f7eef60e6558418d7da6551dd3d07ccc1290ecc85755d781bd8100322ea5b", + "sha256:068e91060e3e211441b1a31f5e65de88fc346490e1fae583c35a75a5295c8ef7", + "sha256:288e8f94fb6f586e7386c1f22c979ce3ec866ab23371fa8fef1dd526cd4dfde1", + "sha256:503ae54582601b0ff647731fee5efcdff5db1f4da0350febb31b628236a5f0b5", + "sha256:6d5f6f26f9025756035c473167b39c5a72e4e519a2286c9399d21f6682e4e5bc", + "sha256:e13265feabb1fa26f9cd49cbafd9b5de70ad768093ddb092af477c9823f44f0e", + "sha256:50de6f3786ba868ffb7d78d4bcacf0928321f9892366b2f4a0426bba644e3f25", + "sha256:16c78b10e897a512aa34ab1969982e42246e53077ae903c1b334926e1ea832d1", + "sha256:e04b5bf8581718cf84c1c60bda40221d926ceb06f942ebabfc3baf467a1e34be", + "sha256:d99819e9e15e1295a31a757360cab65bc96162870f90c29432564bd8e8999aca", + "sha256:cd172509bfc9144395204dd2c0eb305ae5e89f8ad1714ffd7d793607c53c3244", + "sha256:3508bea4974ee30fabcf7c8852fca7d9d54d496eaa068bee8311e0ac4df4ade3", + "sha256:fb4412490324705dcd2172baa8a3ea58ae23c5f982476805cad58ae929fe2a52" + ], + "version": "==4.1.0" + }, "pycodestyle": { "hashes": [ "sha256:6c4245ade1edfad79c3446fadfc96b0de2759662dc29d07d80a6f27ad1ca6ba9", @@ -252,6 +269,64 @@ "sha256:8d616a382f243dbf19b54743f280b80198be0bca3a5396f1d2e1fca6223e8805" ], "version": "==1.6.0" + }, + "pyyaml": { + "hashes": [ + "sha256:3262c96a1ca437e7e4763e2843746588a965426550f3797a79fca9c6199c431f", + "sha256:16b20e970597e051997d90dc2cddc713a2876c47e3d92d59ee198700c5427736", + "sha256:e863072cdf4c72eebf179342c94e6989c67185842d9997960b3e69290b2fa269", + "sha256:bc6bced57f826ca7cb5125a10b23fd0f2fff3b7c4701d64c439a300ce665fff8", + "sha256:c01b880ec30b5a6e6aa67b09a2fe3fb30473008c85cd6a67359a1b15ed6d83a4", + "sha256:827dc04b8fa7d07c44de11fabbc888e627fa8293b695e0f99cb544fdfa1bf0d1", + "sha256:592766c6303207a20efc445587778322d7f73b161bd994f227adaa341ba212ab", + "sha256:5f84523c076ad14ff5e6c037fe1c89a7f73a3e04cf0377cb4d017014976433f3", + "sha256:0c507b7f74b3d2dd4d1322ec8a94794927305ab4cebbe89cc47fe5e81541e6e8", + "sha256:b4c423ab23291d3945ac61346feeb9a0dc4184999ede5e7c43e1ffb975130ae6", + "sha256:ca233c64c6e40eaa6c66ef97058cdc80e8d0157a443655baa1b2966e812807ca", + "sha256:4474f8ea030b5127225b8894d626bb66c01cda098d47a2b0d3429b6700af9fd8", + "sha256:326420cbb492172dec84b0f65c80942de6cedb5233c413dd824483989c000608", + "sha256:5ac82e411044fb129bae5cfbeb3ba626acb2af31a8d17d175004b70862a741a7" + ], + "version": "==3.12" + }, + "six": { + "hashes": [ + "sha256:832dc0e10feb1aa2c68dcc57dbb658f1c7e65b9b61af69048abc87a2db00a0eb", + "sha256:70e8a77beed4562e7f14fe23a786b54f6296e34344c23bc42f07b15018ff98e9" + ], + "version": "==1.11.0" + }, + "vcrpy": { + "hashes": [ + "sha256:b4041abd66e13ef52e498b98ce24f55bcba61cee2fa58569b78889f2c7ba24bb", + "sha256:f434fe7e05d940d576ac850709ae57a738ba40e7f317076ea8d359ced5b32320" + ], + "version": "==1.11.1" + }, + "wrapt": { + "hashes": [ + "sha256:d4d560d479f2c21e1b5443bbd15fe7ec4b37fe7e53d335d3b9b0a7b1226fe3c6" + ], + "version": "==1.10.11" + }, + "yarl": { + "hashes": [ + "sha256:d9ca55a5a297408f08e5401c23ad22bd9f580dab899212f0d5dc1830f0909404", + "sha256:17e57a495efea42bcfca08b49e16c6d89e003acd54c99c903ea1cb3de0ba1248", + "sha256:3353fae45d93cc3e7e41bfcb1b633acc37db821d368e660b03068dbfcf68f8c8", + "sha256:51a084ff8756811101f8b5031a14d1c2dd26c666976e1b18579c6b1c8761a102", + "sha256:045dbba18c9142278113d5dc62622978a6f718ba662392d406141c59b540c514", + "sha256:213e8f54b4a942532d6ac32314c69a147d3b82fa1725ca05061b7c1a19a1d9b1", + "sha256:7236eba4911a5556b497235828e7a4bc5d90957efa63b7c4b3e744d2d2cf1b94", + "sha256:e9a6a319c4bbfb57618f207e86a7c519ab0f637be3d2366e4cdac271577834b8", + "sha256:6e75753065c310befab71c5077a59b7cb638d2146b1cfbb1c3b8f08b51362714", + "sha256:64727a2593fdba5d6ef69e94eba793a196deeda7152c7bd3a64edda6b1f95f6e", + "sha256:5580f22ac1298261cd24e8e584180d83e2cca9a6167113466d2d16cb2aa1f7b1", + "sha256:e072edbd1c5628c0b8f97d00cf6c9fcd6a4ee2b5ded10d463fcb6eaa066cf40c", + "sha256:a69dd7e262cdb265ac7d5e929d55f2f3d07baaadd158c8f19caebf8dde08dfe8" + ], + "markers": "python_version in '3.4, 3.5, 3.6'", + "version": "==1.1.1" } } } diff --git a/project_admin/cassettes/test_invalid_token.yaml b/project_admin/cassettes/test_invalid_token.yaml new file mode 100644 index 0000000..b9cea8c --- /dev/null +++ b/project_admin/cassettes/test_invalid_token.yaml @@ -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 diff --git a/project_admin/cassettes/test_valid_token.yaml b/project_admin/cassettes/test_valid_token.yaml new file mode 100644 index 0000000..2dbef32 --- /dev/null +++ b/project_admin/cassettes/test_valid_token.yaml @@ -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 diff --git a/project_admin/tests.py b/project_admin/tests.py index a79ca8b..31a2246 100644 --- a/project_admin/tests.py +++ b/project_admin/tests.py @@ -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/')