Skip to content

Commit

Permalink
Add property based testing to twins API using schemathesis
Browse files Browse the repository at this point in the history
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
  • Loading branch information
rodneyosodo authored and dborovcanin committed Apr 16, 2024
1 parent f6477ed commit 59642ef
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 137 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/api-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ env:
AUTH_URL: http://localhost:8189
BOOTSTRAP_URL: http://localhost:9013
CERTS_URL: http://localhost:9019
TWINS_URL: http://localhost:9018

jobs:
api-test:
Expand Down Expand Up @@ -181,6 +182,16 @@ jobs:
report: false
args: '--header "Authorization: Bearer ${{ env.USER_TOKEN }}" --contrib-openapi-formats-uuid --hypothesis-suppress-health-check=filter_too_much --stateful=links'

- name: Run Twins API tests
if: steps.changes.outputs.twins == 'true'
uses: schemathesis/action@v1
with:
schema: api/openapi/twins.yml
base-url: ${{ env.TWINS_URL }}
checks: all
report: false
args: '--header "Authorization: Bearer ${{ env.USER_TOKEN }}" --contrib-openapi-formats-uuid --hypothesis-suppress-health-check=filter_too_much --stateful=links'

- name: Stop containers
if: always()
run: make run down args="-v"
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ test_api_invitations: TEST_API_URL := http://localhost:9020
test_api_auth: TEST_API_URL := http://localhost:8189
test_api_bootstrap: TEST_API_URL := http://localhost:9013
test_api_certs: TEST_API_URL := http://localhost:9019
test_api_twins: TEST_API_URL := http://localhost:9018

$(TEST_API):
$(call test_api_service,$(@),$(TEST_API_URL))
Expand Down
164 changes: 98 additions & 66 deletions api/openapi/twins.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ info:
servers:
- url: http://localhost:9018
- url: https://localhost:9018

tags:
- name: twins
description: Everything about your Twins
externalDocs:
description: Find out more about twins
url: https://docs.magistrala.abstractmachines.fr/


paths:
/twins:
post:
operationId: createTwin
summary: Adds new twin
description: |
Adds new twin to the list of twins owned by user identified using
Expand All @@ -39,132 +39,151 @@ paths:
requestBody:
$ref: "#/components/requestBodies/TwinReq"
responses:
'201':
"201":
$ref: "#/components/responses/TwinCreateRes"
'400':
"400":
description: Failed due to malformed JSON.
'401':
"401":
description: Missing or invalid access token provided.
'415':
"415":
description: Missing or invalid content type.
'500':
$ref: '#/components/responses/ServiceError'
"422":
description: Database can't process request.
"500":
$ref: "#/components/responses/ServiceError"

get:
operationId: getTwins
summary: Retrieves twins
description: |
Retrieves a list of twins. Due to performance concerns, data
is retrieved in subsets.
tags:
- twins
parameters:
- $ref: '#/components/parameters/Limit'
- $ref: '#/components/parameters/Offset'
- $ref: '#/components/parameters/Name'
- $ref: '#/components/parameters/Metadata'
- $ref: "#/components/parameters/Limit"
- $ref: "#/components/parameters/Offset"
- $ref: "#/components/parameters/Name"
- $ref: "#/components/parameters/Metadata"
responses:
'200':
$ref: '#/components/responses/TwinsPageRes'
'400':
"200":
$ref: "#/components/responses/TwinsPageRes"
"400":
description: Failed due to malformed query parameters.
'401':
"401":
description: Missing or invalid access token provided.
'500':
$ref: '#/components/responses/ServiceError'
"422":
description: Database can't process request.
"500":
$ref: "#/components/responses/ServiceError"

/twins/{twinID}:
get:
operationId: getTwin
summary: Retrieves twin info
tags:
- twins
parameters:
- $ref: '#/components/parameters/TwinID'
- $ref: "#/components/parameters/TwinID"
responses:
'200':
$ref: '#/components/responses/TwinRes'
'400':
"200":
$ref: "#/components/responses/TwinRes"
"400":
description: Failed due to malformed twin's ID.
'401':
"401":
description: Missing or invalid access token provided.
'404':
"404":
description: Twin does not exist.
'500':
$ref: '#/components/responses/ServiceError'
"422":
description: Database can't process request.
"500":
$ref: "#/components/responses/ServiceError"
put:
operationId: updateTwin
summary: Updates twin info
description: |
Update is performed by replacing the current resource data with values
provided in a request payload. Note that the twin's ID cannot be changed.
tags:
- twins
parameters:
- $ref: '#/components/parameters/TwinID'
- $ref: "#/components/parameters/TwinID"
requestBody:
$ref: '#/components/requestBodies/TwinReq'
$ref: "#/components/requestBodies/TwinReq"
responses:
'200':
"200":
description: Twin updated.
'400':
"400":
description: Failed due to malformed twin's ID or malformed JSON.
'401':
"401":
description: Missing or invalid access token provided.
'404':
"404":
description: Twin does not exist.
'415':
"415":
description: Missing or invalid content type.
'500':
$ref: '#/components/responses/ServiceError'
"422":
description: Database can't process request.
"500":
$ref: "#/components/responses/ServiceError"
delete:
operationId: removeTwin
summary: Removes a twin
description: Removes a twin.
tags:
- twins
parameters:
- $ref: '#/components/parameters/TwinID'
- $ref: "#/components/parameters/TwinID"
responses:
'204':
"204":
description: Twin removed.
'400':
"400":
description: Failed due to malformed twin's ID.
'401':
"401":
description: Missing or invalid access token provided
'404':
"404":
description: Twin does not exist.
'500':
$ref: '#/components/responses/ServiceError'
"415":
description: Missing or invalid content type.
"422":
description: Database can't process request.
"500":
$ref: "#/components/responses/ServiceError"

/states/{twinID}:
get:
operationId: getStates
summary: Retrieves states of twin with id twinID
description: |
Retrieves a list of states. Due to performance concerns, data
is retrieved in subsets.
tags:
- states
parameters:
- $ref: '#/components/parameters/TwinID'
- $ref: '#/components/parameters/Limit'
- $ref: '#/components/parameters/Offset'
- $ref: "#/components/parameters/TwinID"
- $ref: "#/components/parameters/Limit"
- $ref: "#/components/parameters/Offset"
responses:
'200':
$ref: '#/components/responses/StatesPageRes'
'400':
"200":
$ref: "#/components/responses/StatesPageRes"
"400":
description: Failed due to malformed query parameters.
'401':
"401":
description: Missing or invalid access token provided.
'404':
"404":
description: Twin does not exist.
'500':
$ref: '#/components/responses/ServiceError'
"422":
description: Database can't process request.
"500":
$ref: "#/components/responses/ServiceError"
/health:
get:
summary: Retrieves service health check info.
tags:
- health
responses:
'200':
"200":
$ref: "#/components/responses/HealthRes"
'500':
"500":
$ref: "#/components/responses/ServiceError"

components:
Expand Down Expand Up @@ -242,7 +261,7 @@ components:
minItems: 0
uniqueItems: true
items:
$ref: '#/components/schemas/Attribute'
$ref: "#/components/schemas/Attribute"
TwinReqObj:
type: object
properties:
Expand All @@ -253,7 +272,7 @@ components:
type: object
description: Arbitrary, object-encoded twin's data.
definition:
$ref: '#/components/schemas/Definition'
$ref: "#/components/schemas/Definition"
TwinResObj:
type: object
properties:
Expand Down Expand Up @@ -283,7 +302,7 @@ components:
minItems: 0
uniqueItems: true
items:
$ref: '#/components/schemas/Definition'
$ref: "#/components/schemas/Definition"
metadata:
type: object
description: Arbitrary, object-encoded twin's data.
Expand All @@ -295,7 +314,7 @@ components:
minItems: 0
uniqueItems: true
items:
$ref: '#/components/schemas/TwinResObj'
$ref: "#/components/schemas/TwinResObj"
total:
type: integer
description: Total number of items.
Expand Down Expand Up @@ -327,12 +346,12 @@ components:
StatesPage:
type: object
properties:
twins:
states:
type: array
minItems: 0
uniqueItems: true
items:
$ref: '#/components/schemas/State'
$ref: "#/components/schemas/State"
total:
type: integer
description: Total number of items.
Expand All @@ -343,15 +362,15 @@ components:
type: integer
description: Maximum number of items to return in one page.
required:
- twins
- states

requestBodies:
TwinReq:
description: JSON-formatted document describing the twin to create or update.
content:
application/json:
schema:
$ref: '#/components/schemas/TwinReqObj'
$ref: "#/components/schemas/TwinReqObj"
required: true

responses:
Expand All @@ -368,25 +387,38 @@ components:
content:
application/json:
schema:
$ref: '#/components/schemas/TwinResObj'
$ref: "#/components/schemas/TwinResObj"
links:
update:
operationId: updateTwin
parameters:
twinID: $response.body#/id
delete:
operationId: removeTwin
parameters:
twinID: $response.body#/id
states:
operationId: getStates
parameters:
twinID: $response.body#/id
TwinsPageRes:
description: Data retrieved.
content:
application/json:
schema:
$ref: '#/components/schemas/TwinsPage'
$ref: "#/components/schemas/TwinsPage"
StatesPageRes:
description: Data retrieved.
content:
application/json:
schema:
$ref: '#/components/schemas/StatesPage'
$ref: "#/components/schemas/StatesPage"
ServiceError:
description: Unexpected server-side error occurred.
HealthRes:
description: Service Health Check.
content:
application/json:
application/health+json:
schema:
$ref: "./schemas/HealthInfo.yml"

Expand Down
Loading

0 comments on commit 59642ef

Please sign in to comment.