Code Monkey home page Code Monkey logo

sumcalc-testtask's Introduction

Coding Assignment

The goal of this assignment is to build a simple HTTP service (with the appropriate unit tests) with two endpoints:

POST /auth Accepts JSON input in the format:

{"username": "", "password": ""}

and returns JWT OAUTH 2/OIDC token with the username as a subject. The username and the password don't have to be verified, but should not accept empty strings. The JWT token should expire in one hour.

It should return appropriate error status code if the JSON payload is not valid, or the username and password are not valid (are empty)

POST /sum Protected with a valid JWT token, generated by the /auth endpoint, provided as a Bearer Authorization header.

Accepts arbitrary JSON document as payload, which can contain a variety of things: arrays [1,2,3,4], arbitrary objects {"a":1, "b":2, "c":3}, numbers, and strings. The endpoint should find all of the numbers throughout the document and add them together.

For example:

[1,2,3,4] and {"a":6,"b":4} both have a sum of 10. [[[2]]] and {"a":{"b":4},"c":-2} both have a sum of 2. {"a":[-1,1,"dark"]} and [-1,{"a":1, "b":"light"}] both have a sum of 0. [] and {} both have a sum of 0. This is not an exhaustive list of examples. Think about what other edge cases there might be.

The response should be the hex digest of the SHA256 hash of the sum of all numbers in the document. It should return the appropriate error status code if the JWT token or the JSON payload are not valid.


Run

We're using assymetric encryption(ES256) for JWT. Therefore we need to generate keys:

make jwtkeys

Make sure you checked config.yaml before launch.

Start

make serve

or

go run cmd/main.go serve --config config.yaml

You also can build docke container. Dont forget to replace PATH_TO_YOUR_GIT_TOKEN

docker build -t doer-api --build-arg GITHUB_TOKEN="$(cat ~/PATH_TO_YOUR_GIT_TOKEN)" .

Tests:

make test
make opencoverage

API

Auth

POST /auth Get JWT Token by username/password. Credentials must be not empty. Free tokens for everyone!

curl --location --request POST 'localhost:8080/auth' \
--header 'Content-Type: application/json' \
--data-raw '{
    "password":"Alice",
    "username":"1337"
}'

Response

400 Bad Request

{"error":"Unexpected EOF"}

422 Unprocessable Entity

{"error":"Key: 'AuthRequest.Username' Error:Field validation for 'Username' failed on the 'required' tag"}

500 Internal Server Error

{"error":"internal error"}

200 OK

{
    "payload": "eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2Nzc2OTI1MzUsImlhdCI6MTY3NzY4ODkzNSwiaXNzIjoiZG9lci5hdXRoIiwic3ViIjoiMiJ9.IHx0zP00Ef6uAeOAVWSpyDFehYM2OkBazke5m_pTSGoYduipMjp-lvHaUV0REvC19KJjoTE-w_rYE6mFWkECew"
}

Sum

Sum all digits from request. Authorization header must be provided

curl --location --request POST 'localhost:8080/sum' \
--header 'Authorization: eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2Nzc3Njg0MzAsImlhdCI6MTY3Nzc2NDgzMCwiaXNzIjoiZG9lci5hdXRoIiwic3ViIjoiMiJ9.TdAmnKS9ZH-MhIbkWRt5FVn-Xgng_unScbltzznt7LJ_r2VUvzG7BDKuub4V33Z8micD6Hb_N0AiFWrVtTYQvQ' \
--header 'Content-Type: application/json' \
--data-raw '[[[2]]]'

Response

400 Bad Request

{"error":"Unexpected EOF"}

200 OK

{
    "sum": 2,
    "hash": "dbc1b4c900ffe48d575b5da5c638040125f65db0fe3e24494b76ea986457d986"
}

Build Info

We need to know what version of app is currently serving our requests.

GET /build

curl localhost:8080/build

Response

{
    "version": "feature/assignment",
    "commit_hash": "5356d19",
    "build_date": "2023-03-02T20:29:26+0600",
    "go_version": "go1.20.1",
    "os": "linux",
    "arch": "amd64",
    "compiler": "gc"
}

sumcalc-testtask's People

Contributors

bogatyr285 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.