Skip to content

Commit

Permalink
Fix bad JWT tests, add tests for Note.Index
Browse files Browse the repository at this point in the history
  • Loading branch information
josephspurrier committed May 26, 2019
1 parent f7735c1 commit 5f1c2e9
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/app/api/endpoint/note.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (p NoteEndpoint) Index(w http.ResponseWriter, r *http.Request) (int, error)
group := p.Store.Note.NewGroup()
err := p.Store.Note.FindAllByUser(&group, userID)
if err != nil {
return http.StatusBadRequest, errors.New("notes not found")
return http.StatusInternalServerError, err
}

// Copy the items to the JSON model.
Expand Down
64 changes: 64 additions & 0 deletions src/app/api/endpoint/note_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,67 @@ func TestNoteCreateFail(t *testing.T) {
w = tr.SendJSON(t, p, "POST", "/v1/note", form)
assert.Equal(t, http.StatusInternalServerError, w.Code)
}

func TestNoteIndexSuccess(t *testing.T) {
db := testutil.LoadDatabase()
defer testutil.TeardownDatabase(db)
p, _ := boot.TestServices(db)
tr := testrequest.New()

// Get an auth token.
token := auth(t, tr, p)

// Get the notes - there should be none.
tr.Header.Set("Authorization", "Bearer "+token)
w := tr.SendJSON(t, p, "GET", "/v1/note", nil)
rr := new(model.NoteIndexResponse)
err := json.Unmarshal(w.Body.Bytes(), &rr.Body)
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, 0, len(rr.Body.Notes))

// Create a note.
form := url.Values{}
form.Set("message", "foo")
tr.Header.Set("Authorization", "Bearer "+token)
tr.SendJSON(t, p, "POST", "/v1/note", form)

// Create another note.
form = url.Values{}
form.Set("message", "foo2")
tr.Header.Set("Authorization", "Bearer "+token)
tr.SendJSON(t, p, "POST", "/v1/note", form)

// Get the notes.
tr.Header.Set("Authorization", "Bearer "+token)
w = tr.SendJSON(t, p, "GET", "/v1/note", nil)

// Verify the response.
rr = new(model.NoteIndexResponse)
err = json.Unmarshal(w.Body.Bytes(), &rr.Body)
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, 2, len(rr.Body.Notes))
}

func TestNoteIndexFail(t *testing.T) {
db := testutil.LoadDatabase()
defer testutil.TeardownDatabase(db)
p, m := boot.TestServices(db)
tr := testrequest.New()

// Get an auth token.
token := auth(t, tr, p)

// Invalid user.
m.Mock.Add("CTX.UserID", "", false)
tr.Header.Set("Authorization", "Bearer "+token)
w := tr.SendJSON(t, p, "GET", "/v1/note", nil)
assert.Equal(t, http.StatusInternalServerError, w.Code)

// Invalid DB.
m.Mock.Add("NoteStore.FindAllByUser", errors.New("no notes"))
tr.Header.Set("Authorization", "Bearer "+token)
w = tr.SendJSON(t, p, "GET", "/v1/note", nil)
assert.Equal(t, http.StatusInternalServerError, w.Code)
}
10 changes: 7 additions & 3 deletions src/app/api/middleware/jwt/jwt_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jwt_test

import (
"app/api/pkg/mock"
"net/http"
"net/http/httptest"
"strings"
Expand All @@ -26,7 +27,8 @@ func TestWhitelistAllowed(t *testing.T) {
"GET /v1/auth",
}

token := jwt.New([]byte("secret"), whitelist, requestcontext.New())
token := jwt.New([]byte("secret"), whitelist,
requestcontext.New(mock.New(false)))
h := token.Handler(mux)

r := httptest.NewRequest(arr[0], arr[1], nil)
Expand All @@ -52,7 +54,8 @@ func TestWhitelistNotAllowed(t *testing.T) {
"GET /v1/auth",
}

token := jwt.New([]byte("secret"), whitelist, requestcontext.New())
token := jwt.New([]byte("secret"), whitelist,
requestcontext.New(mock.New(false)))
h := token.Handler(mux)

r := httptest.NewRequest(arr[0], arr[1], nil)
Expand All @@ -72,7 +75,8 @@ func TestWhitelistBadBearer(t *testing.T) {
"GET /v1/auth",
}

token := jwt.New([]byte("secret"), whitelist, requestcontext.New())
token := jwt.New([]byte("secret"), whitelist,
requestcontext.New(mock.New(false)))
h := token.Handler(mux)

r := httptest.NewRequest("POST", "/v1/user", nil)
Expand Down

0 comments on commit 5f1c2e9

Please sign in to comment.