Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions tekton/pipeline.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,16 @@ spec:
value: $(params.branch)
runAfter:
- init
- name: lint
workspaces:
- name: source
workspace: pipeline-workspace
taskRef:
name: flake8
params:
- name: image
value: "python:3.9-slim"
- name: args
value: ["--count","--max-complexity=10","--max-line-length=127","--statistics"]
runAfter:
- clone
62 changes: 25 additions & 37 deletions tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
HTTPS_ENVIRON = {'wsgi.url_scheme': 'https'}


######################################################################
# T E S T C A S E S
######################################################################
class TestAccountService(TestCase):
"""Account Service Tests"""

Expand All @@ -40,23 +37,20 @@ def setUpClass(cls):

@classmethod
def tearDownClass(cls):
"""Runs once before test suite"""
"""Run once after all tests"""
pass

def setUp(self):
"""Runs before each test"""
db.session.query(Account).delete() # clean up the last tests
db.session.commit()

self.client = app.test_client()

def tearDown(self):
"""Runs once after each test case"""
db.session.remove()

######################################################################
# H E L P E R M E T H O D S
######################################################################

# Helper method
def _create_accounts(self, count):
"""Factory method to create accounts in bulk"""
accounts = []
Expand All @@ -73,9 +67,7 @@ def _create_accounts(self, count):
accounts.append(account)
return accounts

######################################################################
# A C C O U N T T E S T C A S E S
######################################################################
# --- Account Test Cases ---

def test_index(self):
"""It should get 200_OK from the Home Page"""
Expand All @@ -85,12 +77,12 @@ def test_index(self):
def test_health(self):
"""It should be healthy"""
resp = self.client.get("/health")
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp.status_code, status.HTTP_200_OK)
data = resp.get_json()
self.assertEqual(data["status"], "OK")

def test_create_account(self):
"""It should Create a new Account"""
"""It should create a new Account"""
account = AccountFactory()
response = self.client.post(
BASE_URL,
Expand All @@ -100,7 +92,7 @@ def test_create_account(self):
self.assertEqual(response.status_code, status.HTTP_201_CREATED)

# Make sure location header is set
location = response.headers.get("Location", None)
location = response.headers.get("Location")
self.assertIsNotNone(location)

# Check the data is correct
Expand All @@ -112,12 +104,12 @@ def test_create_account(self):
self.assertEqual(new_account["date_joined"], str(account.date_joined))

def test_bad_request(self):
"""It should not Create an Account when sending the wrong data"""
"""It should not create an Account with wrong data"""
response = self.client.post(BASE_URL, json={"name": "not enough data"})
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

def test_unsupported_media_type(self):
"""It should not Create an Account when sending the wrong media type"""
"""It should not create an Account with wrong media type"""
account = AccountFactory()
response = self.client.post(
BASE_URL,
Expand All @@ -126,46 +118,43 @@ def test_unsupported_media_type(self):
)
self.assertEqual(response.status_code, status.HTTP_415_UNSUPPORTED_MEDIA_TYPE)

# ADD YOUR TEST CASES HERE ...
def test_read_an_account(self):
"""It should Read an existing Account"""
"""It should read an existing Account"""
account = self._create_accounts(1)[0]
response = self.client.get(f"{BASE_URL}/{account.id}")
self.assertEqual(response.status_code, status.HTTP_200_OK)
data = response.get_json()
self.assertEqual(data["id"], account.id)
self.assertEqual(data["name"], account.name)

def test_get_account_not_found(self):
"""It should not Read an Account that is not found"""
"""It should not read an Account that is not found"""
resp = self.client.get(f"{BASE_URL}/0")
self.assertEqual(resp.status_code, status.HTTP_404_NOT_FOUND)

def test_get_account_list(self):
"""It should Get a list of Accounts"""
"""It should get a list of Accounts"""
self._create_accounts(5)
resp = self.client.get(BASE_URL)
self.assertEqual(resp.status_code, status.HTTP_200_OK)
data = resp.get_json()
self.assertEqual(len(data), 5)


self.assertEqual(len(data), 5)

def test_update_account(self):
"""It should Update an existing Account"""
# create an Account to update
"""It should update an existing Account"""
test_account = AccountFactory()
resp = self.client.post(BASE_URL, json=test_account.serialize())
self.assertEqual(resp.status_code, status.HTTP_201_CREATED)

# update the account
new_account = resp.get_json()
new_account["name"] = "Amine"
resp = self.client.put(f"{BASE_URL}/{new_account['id']}", json=new_account)
self.assertEqual(resp.status_code, status.HTTP_200_OK)
updated_account = resp.get_json()
self.assertEqual(updated_account["name"], "Amine")

def test_delete_account(self):
"""It should Delete an Account"""
"""It should delete an Account"""
account = self._create_accounts(1)[0]
resp = self.client.delete(f"{BASE_URL}/{account.id}")
self.assertEqual(resp.status_code, status.HTTP_204_NO_CONTENT)
Expand All @@ -174,23 +163,22 @@ def test_method_not_allowed(self):
"""It should not allow an illegal method call"""
resp = self.client.delete(BASE_URL)
self.assertEqual(resp.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)

def test_security_headers(self):
"""It should return security headers"""
response = self.client.get('/', environ_overrides=HTTPS_ENVIRON)
response = self.client.get("/", environ_overrides=HTTPS_ENVIRON)
self.assertEqual(response.status_code, status.HTTP_200_OK)
headers = {
expected_headers = {
'X-Frame-Options': 'SAMEORIGIN',
'X-Content-Type-Options': 'nosniff',
'Content-Security-Policy': 'default-src \'self\'; object-src \'none\'',
'Content-Security-Policy': "default-src 'self'; object-src 'none'",
'Referrer-Policy': 'strict-origin-when-cross-origin'
}
for key, value in headers.items():
for key, value in expected_headers.items():
self.assertEqual(response.headers.get(key), value)

def test_cors_security(self):
"""It should return a CORS header"""
response = self.client.get('/', environ_overrides=HTTPS_ENVIRON)
response = self.client.get("/", environ_overrides=HTTPS_ENVIRON)
self.assertEqual(response.status_code, status.HTTP_200_OK)
# Check for the CORS header
self.assertEqual(response.headers.get('Access-Control-Allow-Origin'), '*')
self.assertEqual(response.headers.get('Access-Control-Allow-Origin'), "*")