Skip to content

Commit

Permalink
Merge branch 'main' into fix_app_brittle_CORS_config
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua-Douglas committed Nov 25, 2023
2 parents 11b278f + 1ff4320 commit 2db9244
Show file tree
Hide file tree
Showing 18 changed files with 634 additions and 319 deletions.
2 changes: 1 addition & 1 deletion api/openapi_server/openapi/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,4 @@ components:
title: message
type: string
title: ApiResponse
type: object
type: object
6 changes: 3 additions & 3 deletions api/openapi_server/openapi/paths/host.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ get:
content:
application/json:
schema:
$ref: "../openapi.yaml#/components/schemas/ApiResponse"
$ref: "../schemas/_index.yaml#/HostResponse"
x-openapi-router-controller: openapi_server.controllers.host_controller
post:
summary: Create a host
Expand All @@ -28,9 +28,9 @@ post:
- name
responses:
"200":
description: Succes created host
description: Success created host
content:
application/json:
schema:
$ref: "../openapi.yaml#/components/schemas/ApiResponse"
$ref: "../schemas/_index.yaml#/HostResponse"
x-openapi-router-controller: openapi_server.controllers.host_controller
12 changes: 12 additions & 0 deletions api/openapi_server/openapi/schemas/_index.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,15 @@ IntakeResponseValue:
required:
- response_text
- intake_question
HostResponse:
type: object
properties:
id:
title: id
type: integer
name:
title: name
type: string
required:
- id
- name
2 changes: 1 addition & 1 deletion api/requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ typing-extensions==4.7.1
# via
# alembic
# sqlalchemy
urllib3==1.26.16
urllib3==1.26.18
# via
# botocore
# requests
Expand Down
2 changes: 1 addition & 1 deletion api/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ typing-extensions==4.7.1
# via
# alembic
# sqlalchemy
urllib3==1.26.16
urllib3==1.26.18
# via
# botocore
# requests
Expand Down
87 changes: 87 additions & 0 deletions api/tests/test_host_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
from tests import BaseTestCase
import pytest

# Local
from openapi_server.models.database import DataAccessLayer, Host

class TestHostController(BaseTestCase):
"""TestHostController integration test stubs"""

def test_create_host(self):
"""
Test creating a new host using a
simulated post request. Verify that the
response is correct, and that the app
database was properly updated.
"""

NEW_HOST = {
"name" : "new_host"
}

response = self.client.post(
'/api/host',
json=NEW_HOST)

self.assertStatus(response, 201,
f'Response body is: {response.json}')
assert 'name' in response.json
assert 'id' in response.json
assert response.json['name'] == NEW_HOST['name']

with DataAccessLayer.session() as session:
test_host = session.query(Host).filter_by(name=NEW_HOST['name']).first()

assert test_host is not None
assert test_host.name == NEW_HOST['name']

def test_create_host_empty_body(self):
"""
Test creating a new host with an empty JSON body.
This should return an error response.
"""

response = self.client.post(
'/api/host',
json={})

self.assertStatus(response, 400)


def test_create_host_invalid_data(self):
"""
Test creating a new host with invalid data in the request body.
This should return an error response (e.g., 400 Bad Request).
"""
invalid_host_data = {"invalid_field": "value"}

response = self.client.post('/api/host', json=invalid_host_data)

self.assertStatus(response, 400)


def test_get_hosts(self):
"""
Test that checks if a list of 5 Hosts are returned from a GET request.
The 5 test Hosts are created by this test.
"""

with DataAccessLayer.session() as session:
host1 = Host(name="host1")
host2 = Host(name="host2")
host3 = Host(name="host3")
host4 = Host(name="host4")
host5 = Host(name="host5")
session.add_all([host1, host2, host3, host4, host5])
session.commit()

response = self.client.get('/api/host')

self.assertStatus(response, 200,
f'Response body is: {response.json}')

assert isinstance(response.json, list)
assert len(response.json) == 5
for i in range(1, len(response.json) + 1):
assert response.json[i - 1]['name'] == f"host{i}"
assert response.json[i - 1]['id'] == i
Loading

0 comments on commit 2db9244

Please sign in to comment.