Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[auth][test] Add unit test basis for auth service #44

Merged
merged 1 commit into from
Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ type Env struct {
jwtCredential *comm.JWTCredential
}

func Router(env Env) *mux.Router {
r := mux.NewRouter()
r.HandleFunc("/auth", env.authCreateHandler).Methods(http.MethodPost)
r.HandleFunc("/auth", env.authReadHandler).Methods(http.MethodGet)
return r
}

func main() {
configPtr := flag.String("config", "/etc/auth-service/config.json", "configuration filepath")
flag.Parse()
Expand All @@ -48,10 +55,7 @@ func main() {

env := Env{d, c, jwtCredential}

r := mux.NewRouter()
r.HandleFunc("/auth", env.authCreateHandler).Methods(http.MethodPost)
r.HandleFunc("/auth", env.authReadHandler).Methods(http.MethodGet)
log.Fatal(http.ListenAndServe(":82", r))
log.Fatal(http.ListenAndServe(":82", Router(env)))
}

func (env *Env) authCreateHandler(w http.ResponseWriter, r *http.Request) {
Expand Down
125 changes: 125 additions & 0 deletions auth/auth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package main

import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/TempleEight/spec-golang/auth/comm"
"github.com/TempleEight/spec-golang/auth/dao"
"github.com/dgrijalva/jwt-go"
)

type MockAuth struct {
ID int
Email string
Password string
}

type MockDAO struct {
AuthList []MockAuth
}

type MockComm struct{}

func (md *MockDAO) CreateAuth(request dao.AuthCreateRequest) (*dao.Auth, error) {
mockAuth := MockAuth{len(md.AuthList), request.Email, request.Password}
md.AuthList = append(md.AuthList, mockAuth)
return &dao.Auth{
Id: mockAuth.ID,
Email: mockAuth.Email,
Password: mockAuth.Password,
}, nil
}

func (md *MockDAO) ReadAuth(request dao.AuthReadRequest) (*dao.Auth, error) {
for _, auth := range md.AuthList {
if auth.Email == request.Email {
return &dao.Auth{
Id: auth.ID,
Email: auth.Email,
Password: auth.Password,
}, nil
}
}
return nil, dao.ErrAuthNotFound
}

func (mc *MockComm) CreateJWTCredential() (*comm.JWTCredential, error) {
return &comm.JWTCredential{
Key: "MyKey",
Secret: "ShhItsASecret",
}, nil
}

func makeRequest(env Env, method string, url string, body string) (*httptest.ResponseRecorder, error) {
rec := httptest.NewRecorder()
req, err := http.NewRequest(method, url, strings.NewReader(body))
if err != nil {
return nil, err
}

Router(env).ServeHTTP(rec, req)
return rec, nil
}

// Test that an auth can be successfully created
func TestAuthCreateHandlerSucceeds(t *testing.T) {
mockComm := MockComm{}
cred, _ := mockComm.CreateJWTCredential()
mockEnv := Env{
&MockDAO{AuthList: make([]MockAuth, 0)},
&mockComm,
cred,
}

res, err := makeRequest(mockEnv, http.MethodPost, "/auth", `{"email": "jay@test.com", "password": "BlackcurrantCrush123"}`)
if err != nil {
t.Fatalf("Could not make request: %s", err.Error())
}

if res.Code != http.StatusOK {
t.Errorf("Wrong status code %v", res.Code)
}

var decoded map[string]string
err = json.Unmarshal([]byte(res.Body.String()), &decoded)
if err != nil {
t.Fatalf("Could not decode json: %s", err.Error())
}

rawToken, ok := decoded["AccessToken"]
if !ok {
t.Fatalf("Token doesn't contain an access token: %s", err.Error())
}

token, _, err := new(jwt.Parser).ParseUnverified(rawToken, jwt.MapClaims{})
if err != nil {
t.Fatalf("Could not decode JWT: %s", err.Error())
}

claims, ok := token.Claims.(jwt.MapClaims)
c-harding marked this conversation as resolved.
Show resolved Hide resolved
if !ok {
t.Fatalf("Could not decode claims")
}

id, ok := claims["id"]
if !ok {
t.Fatalf("Claims doesn't contain an ID key")
}

if id.(float64) != 0 {
t.Fatalf("ID is incorrect: found %+v, wanted: 0", id)
}

iss, ok := claims["iss"]
if !ok {
t.Fatalf("Claims doesn't contain an iss key")
}

if iss.(string) != cred.Key {
t.Fatalf("iss is incorrect: found %v, wanted: %s", iss, cred.Key)
}
}
4 changes: 2 additions & 2 deletions auth/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ go 1.13

require (
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the incompatible referring to?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dgrijalva/jwt-go#294 Looks like it's a go mod thing, where jwt-go doesn't fully support modules in v3, but will do in v4. Looks like it's in preview for now, but we can upgrade if/when they release, although they mention will be some breaking changes

github.com/gorilla/mux v1.7.4
github.com/lib/pq v1.3.0
golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d // indirect
golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d
)