MCPcopy Index your code
hub / github.com/dnote/dnote / TestGetNote

Function TestGetNote

pkg/server/controllers/notes_test.go:175–302  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

173}
174
175func TestGetNote(t *testing.T) {
176 db := testutils.InitMemoryDB(t)
177
178 // Setup
179 a := app.NewTest()
180 a.DB = db
181 a.Clock = clock.NewMock()
182 server := MustNewServer(t, &a)
183 defer server.Close()
184
185 user := testutils.SetupUserData(db, "user@test.com", "pass1234")
186 anotherUser := testutils.SetupUserData(db, "another@test.com", "pass1234")
187
188 b1 := database.Book{
189 UUID: testutils.MustUUID(t),
190 UserID: user.ID,
191 Label: "js",
192 }
193 testutils.MustExec(t, db.Save(&b1), "preparing b1")
194
195 note := database.Note{
196 UUID: testutils.MustUUID(t),
197 UserID: user.ID,
198 BookUUID: b1.UUID,
199 Body: "note content",
200 }
201 testutils.MustExec(t, db.Save(&note), "preparing note")
202 deletedNote := database.Note{
203 UUID: testutils.MustUUID(t),
204 UserID: user.ID,
205 BookUUID: b1.UUID,
206 Deleted: true,
207 }
208 testutils.MustExec(t, db.Save(&deletedNote), "preparing deletedNote")
209
210 getURL := func(noteUUID string) string {
211 return fmt.Sprintf("/api/v3/notes/%s", noteUUID)
212 }
213
214 t.Run("owner accessing note", func(t *testing.T) {
215 // Execute
216 url := getURL(note.UUID)
217 req := testutils.MakeReq(server.URL, "GET", url, "")
218 res := testutils.HTTPAuthDo(t, db, req, user)
219
220 // Test
221 assert.StatusCodeEquals(t, res, http.StatusOK, "")
222
223 var payload presenters.Note
224 if err := json.NewDecoder(res.Body).Decode(&payload); err != nil {
225 t.Fatal(errors.Wrap(err, "decoding payload"))
226 }
227
228 var noteRecord database.Note
229 testutils.MustExec(t, db.Where("uuid = ?", note.UUID).First(&noteRecord), "finding noteRecord")
230
231 expected := getExpectedNotePayload(noteRecord, b1, user)
232 assert.DeepEqual(t, payload, expected, "payload mismatch")

Callers

nothing calls this directly

Calls 14

InitMemoryDBFunction · 0.92
NewTestFunction · 0.92
NewMockFunction · 0.92
SetupUserDataFunction · 0.92
MustUUIDFunction · 0.92
MustExecFunction · 0.92
MakeReqFunction · 0.92
HTTPAuthDoFunction · 0.92
StatusCodeEqualsFunction · 0.92
DeepEqualFunction · 0.92
HTTPDoFunction · 0.92
MustNewServerFunction · 0.85

Tested by

no test coverage detected