Skip to content

Commit

Permalink
Add property based testing to auth API using schemathesis
Browse files Browse the repository at this point in the history
Signed-off-by: Rodney Osodo <[email protected]>
  • Loading branch information
rodneyosodo committed Feb 21, 2024
1 parent e40bfb6 commit 28c4b00
Show file tree
Hide file tree
Showing 13 changed files with 161 additions and 256 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/api-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ env:
USERS_URL: http://localhost:9002
THINGS_URL: http://localhost:9000
INVITATIONS_URL: http://localhost:9020
AUTH_URL: http://localhost:8189

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

- name: Run Auth API tests
if: steps.changes.outputs.auth == 'true'
uses: schemathesis/action@v1
with:
schema: api/openapi/auth.yml
base-url: ${{ env.AUTH_URL }}
checks: all
report: false
args: '--header "Authorization: Bearer ${{ env.USER_TOKEN }}" --contrib-unique-data --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 @@ -153,6 +153,7 @@ endef
test_api_users: TEST_API_URL := http://localhost:9002
test_api_things: TEST_API_URL := http://localhost:9000
test_api_invitations: TEST_API_URL := http://localhost:9020
test_api_auth: TEST_API_URL := http://localhost:8189

$(TEST_API):
$(call test_api_service,$(@),$(TEST_API_URL))
Expand Down
49 changes: 26 additions & 23 deletions api/openapi/auth.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ paths:
responses:
"200":
$ref: "#/components/responses/DomainsPageRes"
"400":
description: Failed due to malformed query parameters.
"401":
description: Missing or invalid access token provided.
"404":
Expand All @@ -99,6 +101,8 @@ paths:
$ref: "#/components/responses/DomainRes"
"401":
description: Missing or invalid access token provided.
"403":
description: Failed to perform authorization over the entity.
"404":
description: A non-existent entity request.
"422":
Expand Down Expand Up @@ -133,26 +137,7 @@ paths:
description: Missing or invalid content type.
"500":
$ref: "#/components/responses/ServiceError"
delete:
summary: Delete domain for a domain with the given id.
description: |
Delete domain removes a domain with the given id from repo
and removes all the things, channels, assigned users, policies related to this domain.
tags:
- Domains
parameters:
- $ref: "#/components/parameters/DomainID"
security:
- bearerAuth: []
responses:
"204":
description: Domain deleted.
"401":
description: Missing or invalid access token provided.
"403":
description: Unauthorized access to domain id.
"500":
$ref: "#/components/responses/ServiceError"

/domains/{domainID}/permissions:
get:
summary: Retrieves user permissions on domain.
Expand Down Expand Up @@ -319,6 +304,7 @@ paths:
$ref: "#/components/responses/ServiceError"
/keys:
post:
operationId: issueKey
tags:
- Keys
summary: Issue API key
Expand All @@ -341,6 +327,7 @@ paths:

/keys/{keyID}:
get:
operationId: getKey
summary: Gets API key details.
description: |
Gets API key details for the given key.
Expand All @@ -355,10 +342,13 @@ paths:
description: Failed due to malformed query parameters.
"401":
description: Missing or invalid access token provided.
"404":
description: A non-existent entity request.
"500":
$ref: "#/components/responses/ServiceError"

delete:
operationId: revokeKey
summary: Revoke API key
description: |
Revoke API key identified by the given ID.
Expand All @@ -371,11 +361,14 @@ paths:
description: Key revoked.
"401":
description: Missing or invalid access token provided.
"404":
description: A non-existent entity request.
"500":
$ref: "#/components/responses/ServiceError"

/policies:
post:
operationId: addPolicies
summary: Creates new policies.
description: |
Creates new policies. Only admin can use this endpoint. Therefore, you need an authentication token for the admin.
Expand All @@ -393,6 +386,8 @@ paths:
description: Missing or invalid access token provided.
"403":
description: Unauthorized access token provided.
"404":
description: A non-existent entity request.
"409":
description: Failed due to using an existing email address.
"415":
Expand All @@ -402,6 +397,7 @@ paths:

/policies/delete:
post:
operationId: deletePolicies
summary: Deletes policies.
description: |
Deletes policies. Only admin can use this endpoint. Therefore, you need an authentication token for the admin.
Expand All @@ -415,6 +411,8 @@ paths:
description: Policies deleted.
"400":
description: Failed due to malformed JSON.
"404":
description: A non-existent entity request.
"409":
description: Failed due to using an existing email address.
"415":
Expand All @@ -441,7 +439,7 @@ paths:
- bearerAuth: []
responses:
"200":
$ref: "users.yml#/components/responses/UserPageRes"
$ref: "#/components/responses/DomainsPageRes"
"400":
description: Failed due to malformed query parameters.
"401":
Expand Down Expand Up @@ -569,7 +567,7 @@ components:
example: 10
description: Maximum number of items to return in one page.
required:
- domain
- domains
- total
- offset
DomainUpdate:
Expand Down Expand Up @@ -867,11 +865,16 @@ components:
application/json:
schema:
$ref: "#/components/schemas/Key"
links:
revoke:
operationId: revokeKey
parameters:
keyID: $response.body#/id

HealthRes:
description: Service Health Check.
content:
application/json:
application/health+json:
schema:
$ref: "./schemas/HealthInfo.yml"

Expand Down
10 changes: 5 additions & 5 deletions auth/api/http/domains/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func createDomainEndpoint(svc auth.Service) endpoint.Endpoint {
return nil, err
}

return createDomainRes{Data: domain}, nil
return createDomainRes{domain}, nil
}
}

Expand All @@ -44,7 +44,7 @@ func retrieveDomainEndpoint(svc auth.Service) endpoint.Endpoint {
if err != nil {
return nil, err
}
return retrieveDomainRes{Data: domain}, nil
return retrieveDomainRes{domain}, nil
}
}

Expand Down Expand Up @@ -85,7 +85,7 @@ func updateDomainEndpoint(svc auth.Service) endpoint.Endpoint {
return nil, err
}

return updateDomainRes{Data: domain}, nil
return updateDomainRes{domain}, nil
}
}

Expand All @@ -111,7 +111,7 @@ func listDomainsEndpoint(svc auth.Service) endpoint.Endpoint {
if err != nil {
return nil, err
}
return listDomainsRes{Data: dp}, nil
return listDomainsRes{dp}, nil
}
}

Expand Down Expand Up @@ -219,6 +219,6 @@ func listUserDomainsEndpoint(svc auth.Service) endpoint.Endpoint {
if err != nil {
return nil, err
}
return listUserDomainsRes{Data: dp}, nil
return listUserDomainsRes{dp}, nil
}
}
52 changes: 13 additions & 39 deletions auth/api/http/domains/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,7 @@ func TestListDomains(t *testing.T) {
token: validToken,
status: http.StatusOK,
listDomainsRequest: auth.DomainsPage{
Page: auth.Page{
Total: 1,
},
Total: 1,
Domains: []auth.Domain{domain},
},
err: nil,
Expand All @@ -265,9 +263,7 @@ func TestListDomains(t *testing.T) {
desc: "list domains with offset",
token: validToken,
listDomainsRequest: auth.DomainsPage{
Page: auth.Page{
Total: 1,
},
Total: 1,
Domains: []auth.Domain{domain},
},
query: "offset=1",
Expand All @@ -285,9 +281,7 @@ func TestListDomains(t *testing.T) {
desc: "list domains with limit",
token: validToken,
listDomainsRequest: auth.DomainsPage{
Page: auth.Page{
Total: 1,
},
Total: 1,
Domains: []auth.Domain{domain},
},
query: "limit=1",
Expand All @@ -305,9 +299,7 @@ func TestListDomains(t *testing.T) {
desc: "list domains with name",
token: validToken,
listDomainsRequest: auth.DomainsPage{
Page: auth.Page{
Total: 1,
},
Total: 1,
Domains: []auth.Domain{domain},
},
query: "name=domainname",
Expand All @@ -332,9 +324,7 @@ func TestListDomains(t *testing.T) {
desc: "list domains with status",
token: validToken,
listDomainsRequest: auth.DomainsPage{
Page: auth.Page{
Total: 1,
},
Total: 1,
Domains: []auth.Domain{domain},
},
query: "status=enabled",
Expand All @@ -359,9 +349,7 @@ func TestListDomains(t *testing.T) {
desc: "list domains with tags",
token: validToken,
listDomainsRequest: auth.DomainsPage{
Page: auth.Page{
Total: 1,
},
Total: 1,
Domains: []auth.Domain{domain},
},
query: "tag=tag1,tag2",
Expand All @@ -386,9 +374,7 @@ func TestListDomains(t *testing.T) {
desc: "list domains with metadata",
token: validToken,
listDomainsRequest: auth.DomainsPage{
Page: auth.Page{
Total: 1,
},
Total: 1,
Domains: []auth.Domain{domain},
},
query: "metadata=%7B%22domain%22%3A%20%22example.com%22%7D&",
Expand All @@ -413,9 +399,7 @@ func TestListDomains(t *testing.T) {
desc: "list domains with permissions",
token: validToken,
listDomainsRequest: auth.DomainsPage{
Page: auth.Page{
Total: 1,
},
Total: 1,
Domains: []auth.Domain{domain},
},
query: "permission=view",
Expand All @@ -440,9 +424,7 @@ func TestListDomains(t *testing.T) {
desc: "list domains with order",
token: validToken,
listDomainsRequest: auth.DomainsPage{
Page: auth.Page{
Total: 1,
},
Total: 1,
Domains: []auth.Domain{domain},
},
query: "order=name",
Expand All @@ -466,9 +448,7 @@ func TestListDomains(t *testing.T) {
desc: "list domains with dir",
token: validToken,
listDomainsRequest: auth.DomainsPage{
Page: auth.Page{
Total: 1,
},
Total: 1,
Domains: []auth.Domain{domain},
},
query: "dir=asc",
Expand Down Expand Up @@ -1231,9 +1211,7 @@ func TestListDomainsByUserID(t *testing.T) {
token: validToken,
status: http.StatusOK,
listDomainsRequest: auth.DomainsPage{
Page: auth.Page{
Total: 1,
},
Total: 1,
Domains: []auth.Domain{domain},
},
userID: validID,
Expand Down Expand Up @@ -1264,9 +1242,7 @@ func TestListDomainsByUserID(t *testing.T) {
desc: "list domains by user id with offset",
token: validToken,
listDomainsRequest: auth.DomainsPage{
Page: auth.Page{
Total: 1,
},
Total: 1,
Domains: []auth.Domain{domain},
},
query: "offset=1",
Expand All @@ -1285,9 +1261,7 @@ func TestListDomainsByUserID(t *testing.T) {
desc: "list domains by user id with limit",
token: validToken,
listDomainsRequest: auth.DomainsPage{
Page: auth.Page{
Total: 1,
},
Total: 1,
Domains: []auth.Domain{domain},
},
query: "limit=1",
Expand Down
Loading

0 comments on commit 28c4b00

Please sign in to comment.