Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

Commit

Permalink
Update API test workflow and schemas
Browse files Browse the repository at this point in the history
Signed-off-by: Rodney Osodo <[email protected]>
Signed-off-by: rodneyosodo <[email protected]>
Signed-off-by: Rodney Osodo <[email protected]>
  • Loading branch information
rodneyosodo committed Jan 16, 2024
1 parent c63292e commit 35f5b77
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 19 deletions.
97 changes: 95 additions & 2 deletions .github/workflows/api-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,47 @@ on:
push:
branches:
- main
paths:
- ".github/workflows/api-tests.yml"
- "api/**"
- "auth/api/http/**"
- "bootstrap/api**"
- "certs/api/**"
- "consumers/notifiers/api/**"
- "http/api/**"
- "invitations/api/**"
- "provision/api/**"
- "readers/api/**"
- "things/api/**"
- "twins/api/**"
- "users/api/**"

pull_request:
branches:
- main
paths:
- ".github/workflows/api-tests.yml"
- "api/**"
- "auth/api/http/**"
- "bootstrap/api**"
- "certs/api/**"
- "consumers/notifiers/api/**"
- "http/api/**"
- "invitations/api/**"
- "provision/api/**"
- "readers/api/**"
- "things/api/**"
- "twins/api/**"
- "users/api/**"

env:
TOKENS_URL: http://localhost:9002/users/tokens/issue
DOMAINS_URL: http://localhost:8189/domains
USER_IDENTITY: [email protected]
USER_SECRET: 12345678
DOMAIN_NAME: demo-test
USERS_URL: http://localhost:9002
THINGS_URL: http://localhost:9000

jobs:
api-test:
Expand Down Expand Up @@ -44,20 +75,82 @@ jobs:
export USER_TOKEN=$(curl -sSX POST $TOKENS_URL -H "Content-Type: application/json" -d "{\"identity\": \"$USER_IDENTITY\",\"secret\": \"$USER_SECRET\",\"domain_id\": \"$DOMAIN_ID\"}" | jq -r .access_token)
echo "USER_TOKEN=$USER_TOKEN" >> $GITHUB_ENV
- name: Check for changes in specific paths
uses: dorny/paths-filter@v2
id: changes
with:
filters: |
auth:
- ".github/workflows/api-tests.yml"
- "api/openapi/auth.yml"
- "auth/api/http/**"
bootstrap:
- ".github/workflows/api-tests.yml"
- "api/openapi/bootstrap.yml"
- "bootstrap/api/**"
certs:
- ".github/workflows/api-tests.yml"
- "api/openapi/certs.yml"
- "certs/api/**"
notifiers:
- ".github/workflows/api-tests.yml"
- "api/openapi/notifiers.yml"
- "consumers/notifiers/api/**"
http:
- ".github/workflows/api-tests.yml"
- "api/openapi/http.yml"
- "http/api/**"
invitations:
- ".github/workflows/api-tests.yml"
- "api/openapi/invitations.yml"
- "invitations/api/**"
provision:
- ".github/workflows/api-tests.yml"
- "api/openapi/provision.yml"
- "provision/api/**"
readers:
- ".github/workflows/api-tests.yml"
- "api/openapi/readers.yml"
- "readers/api/**"
things:
- ".github/workflows/api-tests.yml"
- "api/openapi/things.yml"
- "things/api/**"
twins:
- ".github/workflows/api-tests.yml"
- "api/openapi/twins.yml"
- "twins/api/**"
users:
- ".github/workflows/api-tests.yml"
- "api/openapi/users.yml"
- "users/api/**"
- name: Run Users API tests
if: steps.changes.outputs.users == 'true'
uses: schemathesis/action@v1
with:
schema: api/openapi/users.yml
base-url: http://localhost:9002
base-url: ${{ env.USERS_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: Run Things API tests
if: steps.changes.outputs.things == 'true'
uses: schemathesis/action@v1
with:
schema: api/openapi/things.yml
base-url: http://localhost:9000
base-url: ${{ env.THINGS_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'
Expand Down
23 changes: 20 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ BUILD_DIR = build
SERVICES = auth users things http coap ws lora influxdb-writer influxdb-reader mongodb-writer \
mongodb-reader cassandra-writer cassandra-reader postgres-writer postgres-reader timescale-writer timescale-reader cli \
bootstrap opcua twins mqtt provision certs smtp-notifier smpp-notifier invitations
TEST_API_SERVICES = auth bootstrap certs http invitations notifiers provision readers things twins users
TEST_API = $(addprefix test_api_,$(TEST_API_SERVICES))
DOCKERS = $(addprefix docker_,$(SERVICES))
DOCKERS_DEV = $(addprefix docker_dev_,$(SERVICES))
CGO_ENABLED ?= 0
Expand Down Expand Up @@ -136,15 +138,30 @@ test: mocks
done
go test -v --race -count 1 -tags test -coverprofile=coverage/coverage.out $$(go list ./... | grep -v 'consumers\|readers\|postgres\|internal\|opcua\|cmd')

test_api:
define test_api_service
$(eval svc=$(subst test_api_,,$(1)))
@which st > /dev/null || (echo "schemathesis not found, please install it from https://github.com/schemathesis/schemathesis#getting-started" && exit 1)
st run api/openapi/users.yml \

@if [ -z "$(USER_TOKEN)" ]; then \
echo "USER_TOKEN is not set"; \
echo "Please set it to a valid token"; \
exit 1; \
fi

st run api/openapi/$(svc).yml \
--checks all \
--base-url http://localhost:9002 \
--base-url $(2) \
--header "Authorization: Bearer $(USER_TOKEN)" \
--contrib-unique-data --contrib-openapi-formats-uuid \
--hypothesis-suppress-health-check=filter_too_much \
--stateful=links
endef

test_api_users: TEST_API_URL := http://localhost:9002
test_api_things: TEST_API_URL := http://localhost:9000

$(TEST_API):
$(call test_api_service,$(@),$(TEST_API_URL))

proto:
protoc -I. --go_out=. --go_opt=paths=source_relative pkg/messaging/*.proto
Expand Down
18 changes: 12 additions & 6 deletions api/openapi/things.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ paths:
"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 All @@ -113,7 +115,7 @@ paths:
requestBody:
$ref: "#/components/requestBodies/ThingsCreateReq"
responses:
"201":
"200":
$ref: "#/components/responses/ThingPageRes"
"400":
description: Failed due to malformed JSON.
Expand Down Expand Up @@ -180,6 +182,8 @@ paths:
description: Failed to perform authorization over the entity.
"404":
description: Failed due to non existing thing.
"409":
description: Failed due to using an existing identity.
"415":
description: Missing or invalid content type.
"422":
Expand Down Expand Up @@ -537,6 +541,8 @@ paths:
description: Failed to perform authorization over the entity.
"404":
description: Channel does not exist.
"409":
description: Failed due to using an existing identity.
"415":
description: Missing or invalid content type.
"422":
Expand Down Expand Up @@ -665,7 +671,7 @@ paths:
security:
- bearerAuth: []
responses:
"200":
"204":
description: Thing unshared.
"400":
description: Failed due to malformed thing's ID.
Expand Down Expand Up @@ -725,7 +731,7 @@ paths:
security:
- bearerAuth: []
responses:
"200":
"204":
description: Thing unshared.
"400":
description: Failed due to malformed thing's ID.
Expand Down Expand Up @@ -1359,7 +1365,7 @@ components:
ChannelsPage:
type: object
properties:
channels:
groups:
type: array
minItems: 0
uniqueItems: true
Expand All @@ -1377,9 +1383,9 @@ components:
example: 10
description: Maximum number of items to return in one page.
required:
- channels
- groups
- total
- level
- offset

PoliciesPage:
type: object
Expand Down
2 changes: 1 addition & 1 deletion internal/api/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func TestEncodeError(t *testing.T) {
svcerr.ErrViewEntity,
svcerr.ErrRemoveEntity,
},
code: http.StatusInternalServerError,
code: http.StatusUnprocessableEntity,
},
{
desc: "InternalServerError",
Expand Down
5 changes: 2 additions & 3 deletions internal/groups/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/absmach/magistrala/internal/groups"
"github.com/absmach/magistrala/internal/testsutil"
"github.com/absmach/magistrala/pkg/clients"
mgclients "github.com/absmach/magistrala/pkg/clients"
"github.com/absmach/magistrala/pkg/errors"
mggroups "github.com/absmach/magistrala/pkg/groups"
"github.com/absmach/magistrala/pkg/groups/mocks"
Expand Down Expand Up @@ -585,7 +584,7 @@ func TestEnableGroup(t *testing.T) {
retrieveResp: mggroups.Group{
Status: clients.Status(groups.EnabledStatus),
},
err: mgclients.ErrStatusAlreadyAssigned,
err: errors.ErrStatusAlreadyAssigned,
},
{
desc: "with retrieve error",
Expand Down Expand Up @@ -685,7 +684,7 @@ func TestDisableGroup(t *testing.T) {
retrieveResp: mggroups.Group{
Status: clients.Status(groups.DisabledStatus),
},
err: mgclients.ErrStatusAlreadyAssigned,
err: errors.ErrStatusAlreadyAssigned,
},
{
desc: "with retrieve error",
Expand Down
4 changes: 2 additions & 2 deletions things/api/http/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ var (

type pageRes struct {
Limit uint64 `json:"limit,omitempty"`
Offset uint64 `json:"offset,omitempty"`
Total uint64 `json:"total,omitempty"`
Offset uint64 `json:"offset"`
Total uint64 `json:"total"`
}

type createClientRes struct {
Expand Down
4 changes: 2 additions & 2 deletions things/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func (svc service) listUserThingPermission(ctx context.Context, userID, thingID
ObjectType: auth.ThingType,
})
if err != nil {
return []string{}, err
return []string{}, errors.Wrap(svcerr.ErrAuthorization, err)
}
return lp.GetPermissions(), nil
}
Expand Down Expand Up @@ -610,7 +610,7 @@ func (svc *service) authorize(ctx context.Context, domainID, subjType, subjKind,
}
res, err := svc.auth.Authorize(ctx, req)
if err != nil {
return "", err
return "", errors.Wrap(errors.ErrAuthorization, err)
}
if !res.GetAuthorized() {
return "", errors.Wrap(errors.ErrAuthorization, err)
Expand Down

0 comments on commit 35f5b77

Please sign in to comment.