Skip to content

Commit

Permalink
Merge pull request #43 from rarimo/fix/requests_test
Browse files Browse the repository at this point in the history
Add autotests
  • Loading branch information
violog authored Jul 9, 2024
2 parents 7e3e17a + a70353d commit f683624
Show file tree
Hide file tree
Showing 14 changed files with 1,534 additions and 88 deletions.
17 changes: 17 additions & 0 deletions Dockerfile.testing
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM golang:1.22-alpine3.20 as buildbase

RUN apk add git build-base

WORKDIR /go/src/github.com/rarimo/rarime-points-svc
COPY . .
COPY tests/mocked ./internal/service/handlers
RUN GOOS=linux go build -o /usr/local/bin/rarime-points-svc /go/src/github.com/rarimo/rarime-points-svc


FROM alpine:3.20

COPY --from=buildbase /usr/local/bin/rarime-points-svc /usr/local/bin/rarime-points-svc
COPY --from=buildbase /go/src/github.com/rarimo/rarime-points-svc/verification_key.json /verification_key.json
RUN apk add --no-cache ca-certificates

ENTRYPOINT ["rarime-points-svc"]
58 changes: 8 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,53 +90,11 @@ For services, we do use ***PostgresSQL*** database.
You can [install it locally](https://www.postgresql.org/download/) or use [docker image](https://hub.docker.com/_/postgres/).

## Testing
To run tests need to mock some logic in handlers/middleware:
```go
func AuthMiddleware(auth *auth.Client, log *logan.Entry) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// claims, err := auth.ValidateJWT(r)
// if err != nil {
// log.WithError(err).Info("Got invalid auth or validation error")
// ape.RenderErr(w, problems.Unauthorized())
// return
// }

// if len(claims) == 0 {
// ape.RenderErr(w, problems.Unauthorized())
// return
// }

ctx := CtxUserClaims([]resources.Claim{{Nullifier: r.Header.Get("nullifier")}})(r.Context())
next.ServeHTTP(w, r.WithContext(ctx))
})
}
}
```
and in handlers/verify_passport:
```go
// proof.PubSignals[zk.Nullifier] = mustHexToInt(nullifier)
// err = Verifier(r).VerifyProof(*proof)
// if err != nil {
// if errors.Is(err, identity.ErrContractCall) {
// Log(r).WithError(err).Error("Failed to verify proof")
// return nil, append(errs, problems.InternalError())
// }
// return nil, problems.BadRequest(err)
// }
```
and in handlers/withdraw(lines 49-58):
```go
// validated in requests.NewWithdraw
// addr, _ := cosmos.AccAddressFromBech32(req.Data.Attributes.Address)
// never panics because of request validation
// proof.PubSignals[zk.Nullifier] = mustHexToInt(nullifier)

// err = Verifier(r).VerifyProof(proof, zk.WithEventData(addr))
// if err != nil {
// ape.RenderErr(w, problems.BadRequest(err)...)
// return
// }
```

Run service with config-testing.yaml (you need to configure db url) and run tests.
In order to run the tests, you need to be in the project directory, have docker and docker compose installed. To start, you need to run the run_tests.sh script.

The script works as follows:
1. If there is already an image rarime-points-svc:test - deletes it.
2. Builds a new rarime-points-svc:test image (some logic mocked)
3. Starts docker compose with test service and database.
4. Runs tests
5. Deletes created containers and everything related to them
31 changes: 31 additions & 0 deletions docker-compose.testing.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
version: "3.9"

services:
rarime-points:
image: rarime-points-svc:test
restart: on-failure
depends_on:
- rarime-points-db
ports:
- "8000:8000"
volumes:
- ./tests/config-testing-0.yaml:/config.yaml
environment:
- KV_VIPER_FILE=/config.yaml
entrypoint: sh -c "rarime-points-svc migrate up && rarime-points-svc run service"

rarime-points-db:
image: postgres:16-alpine3.20
restart: unless-stopped
ports:
- "5454:5432"
environment:
- POSTGRES_USER=points
- POSTGRES_PASSWORD=points
- POSTGRES_DB=points
- PGDATA=/pgdata
volumes:
- points-data:/pgdata

volumes:
points-data:
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ require (
github.com/rarimo/saver-grpc-lib v1.0.0
github.com/rarimo/zkverifier-kit v1.0.0
github.com/rubenv/sql-migrate v1.6.1
github.com/stretchr/testify v1.9.0
gitlab.com/distributed_lab/ape v1.7.1
gitlab.com/distributed_lab/figure v2.1.2+incompatible
gitlab.com/distributed_lab/figure/v3 v3.1.4
gitlab.com/distributed_lab/json-api-connector v0.2.7
gitlab.com/distributed_lab/kit v1.11.3
Expand Down Expand Up @@ -137,7 +139,6 @@ require (
github.com/spf13/cobra v1.7.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.18.2 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/supranational/blst v0.3.11 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect
Expand All @@ -150,7 +151,6 @@ require (
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/zondax/hid v0.9.1 // indirect
github.com/zondax/ledger-go v0.14.1 // indirect
gitlab.com/distributed_lab/figure v2.1.2+incompatible // indirect
gitlab.com/distributed_lab/lorem v0.2.1 // indirect
go.etcd.io/bbolt v1.3.7 // indirect
go.uber.org/multierr v1.11.0 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2122,8 +2122,6 @@ github.com/rarimo/decentralized-auth-svc v0.0.0-20240522134350-2694eafa9509 h1:U
github.com/rarimo/decentralized-auth-svc v0.0.0-20240522134350-2694eafa9509/go.mod h1:V9XSqZSBN/YmLdI6PW6GL2xNeJ94IXAnhcuvyQfVBL8=
github.com/rarimo/saver-grpc-lib v1.0.0 h1:MGUVjYg7unmodYczVsLqlqZNkT4CIgKqdo6aQtL1qdE=
github.com/rarimo/saver-grpc-lib v1.0.0/go.mod h1:DpugWK5B7Hi0bdC3MPe/9FD2zCxaRwsyykdwxtF1Zgg=
github.com/rarimo/zkverifier-kit v0.2.2 h1:U2NrGQicGN/dNxYgEzbKO061ooCu2LTDoVL8cOLJdHw=
github.com/rarimo/zkverifier-kit v0.2.2/go.mod h1:3YDg5dTkDRr4IdfaDHGYetopd6gS/2SuwSeseYTWwNw=
github.com/rarimo/zkverifier-kit v1.0.0 h1:zMW85hyDP3Uk6p9Dk9U4TBzOf0Pry+RNlWpli1tUZ1Q=
github.com/rarimo/zkverifier-kit v1.0.0/go.mod h1:3YDg5dTkDRr4IdfaDHGYetopd6gS/2SuwSeseYTWwNw=
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 h1:N/ElC8H3+5XpJzTSTfLsJV/mx9Q9g7kxmchpfZyxgzM=
Expand Down
Loading

0 comments on commit f683624

Please sign in to comment.