Skip to content

Commit 3f59893

Browse files
committed
Added circleci config, pycodestyle config, pytest config, tests
1 parent 813e489 commit 3f59893

File tree

5 files changed

+156
-0
lines changed

5 files changed

+156
-0
lines changed

.circleci/config.yml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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:3.6.1
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+
python3 -m venv 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+
python manage.py test
52+
53+
- store_artifacts:
54+
path: test-reports
55+
destination: test-reports

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"

tests/__init__.py

Whitespace-only changes.

tests/test_authentication.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
@patch('os.getenv')
53+
def test_missing_base_url(self, mock_getenv):
54+
mock_getenv.side_effect = [
55+
"test_client_id",
56+
"test_username",
57+
"test_password",
58+
None
59+
]
60+
with self.assertRaises(DemandAPIError):
61+
DemandAPIClient()
62+
63+
64+
def api_test_url(endpoint):
65+
return "{}{}".format(BASE_URL, endpoint)
66+
67+
68+
class AuthenticationTests(unittest.TestCase):
69+
@patch('os.getenv')
70+
def setUp(self, mock_getenv):
71+
mock_getenv.side_effect = [
72+
"test_client_id",
73+
"test_username",
74+
"test_password",
75+
BASE_URL
76+
]
77+
self.client = DemandAPIClient()
78+
self.assertEqual(self.client.client_id, "test_client_id")
79+
80+
@responses.activate
81+
def test_authenticate(self):
82+
responses.add(
83+
responses.POST,
84+
"{}{}".format(self.client.auth_base_url, "/token/password"),
85+
json={
86+
"accessToken": "access_token",
87+
"refreshToken": "refresh_token"
88+
}
89+
)
90+
self.client.authenticate()
91+
self.assertEqual(self.client._access_token, "access_token")
92+
self.assertEqual(self.client._refresh_token, "refresh_token")
93+
self.assertIsNone(self.client._check_authentication())

0 commit comments

Comments
 (0)