Skip to content

Commit 6b1627a

Browse files
authored
Merge branch 'feature/add-authentication' into feature/add-authentication
2 parents fc4104e + fb7d423 commit 6b1627a

File tree

8 files changed

+154
-2
lines changed

8 files changed

+154
-2
lines changed

.circleci/config.yml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Python CircleCI 2.0 configuration file
2+
#
3+
# Check https://circleci.com/docs/2.0/language-python/ for more details
4+
#
5+
version: 2
6+
jobs:
7+
build:
8+
docker:
9+
# specify the version you desire here
10+
# use `-browsers` prefix for selenium tests, e.g. `3.6.1-browsers`
11+
- image: circleci/python:2.7.17
12+
13+
# Specify service dependencies here if necessary
14+
# CircleCI maintains a library of pre-built images
15+
# documented at https://circleci.com/docs/2.0/circleci-images/
16+
# - image: circleci/postgres:9.4
17+
18+
working_directory: ~/repo
19+
20+
steps:
21+
- checkout
22+
23+
# Download and cache dependencies
24+
- restore_cache:
25+
keys:
26+
- v1-dependencies-{{ checksum "requirements.txt" }}
27+
# fallback to using the latest cache if no exact match is found
28+
- v1-dependencies-
29+
30+
- run:
31+
name: install dependencies
32+
command: |
33+
virtualenv venv
34+
. venv/bin/activate
35+
pip install -r requirements.txt
36+
37+
- save_cache:
38+
paths:
39+
- ./venv
40+
key: v1-dependencies-{{ checksum "requirements.txt" }}
41+
42+
# run tests!
43+
# this example uses Django's built-in test-runner
44+
# other common Python testing frameworks include pytest and nose
45+
# https://pytest.org
46+
# https://nose.readthedocs.io
47+
- run:
48+
name: run tests
49+
command: |
50+
. venv/bin/activate
51+
pytest tests
52+
53+
- store_artifacts:
54+
path: test-reports
55+
destination: test-reports
56+
57+
- store_test_results:
58+
path: test-reports

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ Information on [contributing](CONTRIBUTING.md).
1717

1818
## Testing
1919

20-
<need some>
20+
Use the command `pytest tests` to run the tests for this project.

dynatademand/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def __init__(self):
1212
raise DemandAPIError("All authentication data is required.")
1313
self._access_token = None
1414
self._refresh_token = None
15-
self.base_host = os.getenv('DYNATA_DEMAND_BASE_URL', 'https://api.researchnow.com')
15+
self.base_host = os.getenv('DYNATA_DEMAND_BASE_URL', default='https://api.researchnow.com')
1616
self.auth_base_url = '{}/auth/v1'.format(self.base_host)
1717
self.base_url = '{}/sample/v1'.format(self.base_host)
1818

pycodestyle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[pycodestyle]
2+
ignore = E501,E722
3+
statistics = True

pytest.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[pytest]
2+
env=
3+
DYNATA_DEMAND_CLIENT_ID="test_client_id"
4+
DYNATA_DEMAND_USERNAME="test_username"
5+
DYNATA_DEMAND_PASSWORD="test_password"

requirements.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pytest==4.6.6
2+
pytest-runner==5.2
3+
requests==2.22.0
4+
responses==0.10.6

tests/__init__.py

Whitespace-only changes.

tests/test_authentication.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# encoding: utf-8
2+
from __future__ import unicode_literals, print_function
3+
4+
import unittest
5+
import responses
6+
7+
try:
8+
from unittest.mock import patch
9+
except ImportError:
10+
from mock import patch
11+
12+
from dynatademand.api import DemandAPIClient
13+
from dynatademand.errors import DemandAPIError
14+
15+
BASE_URL = "http://test-url.example"
16+
17+
18+
class AuthenticationTestMissingCredentials(unittest.TestCase):
19+
@patch('os.getenv')
20+
def test_missing_client_id(self, mock_getenv):
21+
mock_getenv.side_effect = [
22+
None,
23+
"test_username",
24+
"test_password",
25+
BASE_URL
26+
]
27+
with self.assertRaises(DemandAPIError):
28+
DemandAPIClient()
29+
30+
@patch('os.getenv')
31+
def test_missing_username(self, mock_getenv):
32+
mock_getenv.side_effect = [
33+
"test_client_id",
34+
None,
35+
"test_password",
36+
BASE_URL
37+
]
38+
with self.assertRaises(DemandAPIError):
39+
DemandAPIClient()
40+
41+
@patch('os.getenv')
42+
def test_missing_password(self, mock_getenv):
43+
mock_getenv.side_effect = [
44+
"test_client_id",
45+
"test_username",
46+
None,
47+
BASE_URL
48+
]
49+
with self.assertRaises(DemandAPIError):
50+
DemandAPIClient()
51+
52+
53+
def api_test_url(endpoint):
54+
return "{}{}".format(BASE_URL, endpoint)
55+
56+
57+
class AuthenticationTests(unittest.TestCase):
58+
@patch('os.getenv')
59+
def setUp(self, mock_getenv):
60+
mock_getenv.side_effect = [
61+
"test_client_id",
62+
"test_username",
63+
"test_password",
64+
BASE_URL
65+
]
66+
self.client = DemandAPIClient()
67+
self.assertEqual(self.client.client_id, "test_client_id")
68+
69+
@responses.activate
70+
def test_authenticate(self):
71+
responses.add(
72+
responses.POST,
73+
"{}{}".format(self.client.auth_base_url, "/token/password"),
74+
json={
75+
"accessToken": "access_token",
76+
"refreshToken": "refresh_token"
77+
}
78+
)
79+
self.client.authenticate()
80+
self.assertEqual(self.client._access_token, "access_token")
81+
self.assertEqual(self.client._refresh_token, "refresh_token")
82+
self.assertIsNone(self.client._check_authentication())

0 commit comments

Comments
 (0)