Skip to content

Commit

Permalink
Merge pull request Nordstrom#37 from tylerdavisnordstrom/server_metal…
Browse files Browse the repository at this point in the history
…inter

Fix server metalinter warnings and tests
  • Loading branch information
SleepyBrett authored Jan 10, 2018
2 parents 1396cfe + 41938a1 commit 09fea01
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
14 changes: 12 additions & 2 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,13 @@ func (app *app) exchangeHandler(writer http.ResponseWriter, request *http.Reques
return
}

writer.Write([]byte(jwt))
_, e := writer.Write([]byte(jwt))
if e != nil {
cliToServerErrorCounter.Inc()
log.Printf("unable to write jwt token: %v ", e)
http.Error(writer, "unable to send token", http.StatusInternalServerError)
return
}

elapsedTime := time.Since(startTime)
elapsedSec := elapsedTime / time.Second
Expand All @@ -214,7 +220,11 @@ func (rv *redisValues) setToken(jwt, token string) error {
// Generate SHA sum for JWT
func (rv *redisValues) generateToken(jwt string) (string, error) {
hash := sha1.New()
hash.Write([]byte(jwt))
_, e := hash.Write([]byte(jwt))
if e != nil {
log.Printf("error hashing jwt: %v ", e)
return "", e
}
token := hash.Sum(nil)
tokenCounter.Inc()
if err := rv.setToken(jwt, fmt.Sprintf("%x", token)); err != nil {
Expand Down
37 changes: 27 additions & 10 deletions cmd/server/main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"log"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -30,13 +31,13 @@ func TestServerSpecs(t *testing.T) {
request, _ := http.NewRequest("GET", url, nil)
resp, _ := app.authClient.client.Do(request)
log.Print(resp.StatusCode)
resp.Body.Close()
resp.Body.Close() // nolint: errcheck
So(resp.StatusCode, ShouldEqual, 303)
})
Convey("should return a 400 error if the port is missing", func() {
url := unitTestServer.URL + "/login?port="
resp, _ := http.Get(url)
resp.Body.Close()
resp.Body.Close() // nolint: errcheck
So(resp.StatusCode, ShouldEqual, 400)
})
})
Expand All @@ -45,22 +46,22 @@ func TestServerSpecs(t *testing.T) {
url := unitTestServer.URL + "/callback"
request, _ := http.NewRequest("GET", url, nil)
response, _ := app.authClient.client.Do(request)
response.Body.Close()
response.Body.Close() // nolint: errcheck
So(response.StatusCode, ShouldEqual, http.StatusBadRequest)
})
Convey("should return a internal server error if the authcode is not valid", func() {
fakeCodeURL := unitTestServer.URL + "/callback?code=asdf123&state=3000"
request, _ := http.NewRequest("GET", fakeCodeURL, nil)
response, _ := app.authClient.client.Do(request)
response.Body.Close()
response.Body.Close() // nolint: errcheck
So(response.StatusCode, ShouldEqual, http.StatusInternalServerError)
})
})
Convey("defaultHandler", func() {
Convey("should have basic html written on the page i.e., body is not nil", func() {
request, _ := http.NewRequest("GET", unitTestServer.URL, nil)
response, _ := app.authClient.client.Do(request)
response.Body.Close()
response.Body.Close() // nolint: errcheck
So(response.Body, ShouldNotEqual, nil)
})
Convey("should return a 200 status code upon a successful connection", func() {
Expand All @@ -70,10 +71,14 @@ func TestServerSpecs(t *testing.T) {
})
Convey("exchangeHandler", func() {
Convey("should return a internal server error due to Redis not being available", func() {
app.redisValues.makeRedisClient()
err := app.redisValues.makeRedisClient()
if err != nil {
fmt.Printf("failed to create redis client: %v ", err)
return
}
exchangeURL := unitTestServer.URL + "/exchange?token=hoopla"
response, _ := http.Get(exchangeURL)
response.Body.Close()
response.Body.Close() // nolint: errcheck
So(response.StatusCode, ShouldEqual, http.StatusUnauthorized)
})
})
Expand Down Expand Up @@ -120,7 +125,11 @@ func TestGenerateToken(t *testing.T) {
rv := setRedisValues(os.Getenv("REDIS_ADDR"), os.Getenv("REDIS_PASSWORD"), redisTTL)
oidcClient := newAuthClient(os.Getenv("CLIENT_ID"), os.Getenv("CLIENT_SECRET"), os.Getenv("REDIRECT_URL"), &oidc.Provider{}, "groupsClaim", "userClaim")
app := setAppMemberFields(rv, oidcClient)
app.redisValues.makeRedisClient()
err := app.redisValues.makeRedisClient()
if err != nil {
fmt.Printf("failed to create redis client: %v ", err)
return
}
Convey("should pass since we are just returning a string", func() {
token, _ := app.redisValues.generateToken("hoopla")
So(token, ShouldNotEqual, nil)
Expand All @@ -134,7 +143,11 @@ func TestFetchJWTForToken(t *testing.T) {
rv := setRedisValues(os.Getenv("REDIS_ADDR"), os.Getenv("REDIS_PASSWORD"), redisTTL)
oidcClient := newAuthClient(os.Getenv("CLIENT_ID"), os.Getenv("CLIENT_SECRET"), os.Getenv("REDIRECT_URL"), &oidc.Provider{}, "groupsClaim", "userClaim")
app := setAppMemberFields(rv, oidcClient)
app.redisValues.makeRedisClient()
err := app.redisValues.makeRedisClient()
if err != nil {
fmt.Printf("failed to create redis client: %v ", err)
return
}
Convey("should error out since we can't access the Redis cache offline", func() {
_, err := app.redisValues.fetchJWTForToken("hoopla")
So(err, ShouldNotEqual, nil)
Expand All @@ -148,7 +161,11 @@ func TestGenerateSendBackURL(t *testing.T) {
rv := setRedisValues(os.Getenv("REDIS_ADDR"), os.Getenv("REDIS_PASSWORD"), redisTTL)
oidcClient := newAuthClient(os.Getenv("CLIENT_ID"), os.Getenv("CLIENT_SECRET"), os.Getenv("REDIRECT_URL"), &oidc.Provider{}, "groupsClaim", "userClaim")
app := setAppMemberFields(rv, oidcClient)
app.redisValues.makeRedisClient()
err := app.redisValues.makeRedisClient()
if err != nil {
fmt.Printf("failed to create redis client: %v ", err)
return
}
Convey("should pass since we will encounter errors when trying to add our value to Redis", func() {
_, err := app.redisValues.generateSendBackURL("hoopla", "3000")
log.Printf("The err is %s", err)
Expand Down

0 comments on commit 09fea01

Please sign in to comment.