mirror of
https://github.com/dnote/dnote
synced 2026-03-15 06:55:49 +01:00
Remove public from notes
This commit is contained in:
parent
bb84369611
commit
fe940aa2ce
13 changed files with 43 additions and 320 deletions
|
|
@ -30,7 +30,7 @@ import (
|
|||
|
||||
// CreateNote creates a note with the next usn and updates the user's max_usn.
|
||||
// It returns the created note.
|
||||
func (a *App) CreateNote(user database.User, bookUUID, content string, addedOn *int64, editedOn *int64, public bool, client string) (database.Note, error) {
|
||||
func (a *App) CreateNote(user database.User, bookUUID, content string, addedOn *int64, editedOn *int64, client string) (database.Note, error) {
|
||||
tx := a.DB.Begin()
|
||||
|
||||
nextUSN, err := incrementUserUSN(tx, user.ID)
|
||||
|
|
@ -66,7 +66,6 @@ func (a *App) CreateNote(user database.User, bookUUID, content string, addedOn *
|
|||
EditedOn: noteEditedOn,
|
||||
USN: nextUSN,
|
||||
Body: content,
|
||||
Public: public,
|
||||
Client: client,
|
||||
}
|
||||
if err := tx.Create(¬e).Error; err != nil {
|
||||
|
|
@ -83,7 +82,6 @@ func (a *App) CreateNote(user database.User, bookUUID, content string, addedOn *
|
|||
type UpdateNoteParams struct {
|
||||
BookUUID *string
|
||||
Content *string
|
||||
Public *bool
|
||||
}
|
||||
|
||||
// GetBookUUID gets the bookUUID from the UpdateNoteParams
|
||||
|
|
@ -104,15 +102,6 @@ func (r UpdateNoteParams) GetContent() string {
|
|||
return *r.Content
|
||||
}
|
||||
|
||||
// GetPublic gets the public field from the UpdateNoteParams
|
||||
func (r UpdateNoteParams) GetPublic() bool {
|
||||
if r.Public == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return *r.Public
|
||||
}
|
||||
|
||||
// UpdateNote creates a note with the next usn and updates the user's max_usn
|
||||
func (a *App) UpdateNote(tx *gorm.DB, user database.User, note database.Note, p *UpdateNoteParams) (database.Note, error) {
|
||||
nextUSN, err := incrementUserUSN(tx, user.ID)
|
||||
|
|
@ -126,9 +115,6 @@ func (a *App) UpdateNote(tx *gorm.DB, user database.User, note database.Note, p
|
|||
if p.Content != nil {
|
||||
note.Body = p.GetContent()
|
||||
}
|
||||
if p.Public != nil {
|
||||
note.Public = p.GetPublic()
|
||||
}
|
||||
|
||||
note.USN = nextUSN
|
||||
note.EditedOn = a.Clock.Now().UnixNano()
|
||||
|
|
|
|||
|
|
@ -91,7 +91,7 @@ func TestCreateNote(t *testing.T) {
|
|||
a.DB = db
|
||||
a.Clock = mockClock
|
||||
|
||||
if _, err := a.CreateNote(user, b1.UUID, "note content", tc.addedOn, tc.editedOn, false, ""); err != nil {
|
||||
if _, err := a.CreateNote(user, b1.UUID, "note content", tc.addedOn, tc.editedOn, ""); err != nil {
|
||||
t.Fatal(errors.Wrapf(err, "creating note for test case %d", idx))
|
||||
}
|
||||
|
||||
|
|
@ -139,7 +139,7 @@ func TestCreateNote_EmptyBody(t *testing.T) {
|
|||
a.Clock = clock.NewMock()
|
||||
|
||||
// Create note with empty body
|
||||
note, err := a.CreateNote(user, b1.UUID, "", nil, nil, false, "")
|
||||
note, err := a.CreateNote(user, b1.UUID, "", nil, nil, "")
|
||||
if err != nil {
|
||||
t.Fatal(errors.Wrap(err, "creating note with empty body"))
|
||||
}
|
||||
|
|
@ -188,7 +188,6 @@ func TestUpdateNote(t *testing.T) {
|
|||
|
||||
c := clock.NewMock()
|
||||
content := "updated test content"
|
||||
public := true
|
||||
|
||||
a := NewTest()
|
||||
a.DB = db
|
||||
|
|
@ -197,7 +196,6 @@ func TestUpdateNote(t *testing.T) {
|
|||
tx := db.Begin()
|
||||
if _, err := a.UpdateNote(tx, user, note, &UpdateNoteParams{
|
||||
Content: &content,
|
||||
Public: &public,
|
||||
}); err != nil {
|
||||
tx.Rollback()
|
||||
t.Fatal(errors.Wrap(err, "updating note"))
|
||||
|
|
@ -218,7 +216,6 @@ func TestUpdateNote(t *testing.T) {
|
|||
assert.Equal(t, noteCount, int64(1), "note count mismatch")
|
||||
assert.Equal(t, noteRecord.UserID, user.ID, "note UserID mismatch")
|
||||
assert.Equal(t, noteRecord.Body, content, "note Body mismatch")
|
||||
assert.Equal(t, noteRecord.Public, public, "note Public mismatch")
|
||||
assert.Equal(t, noteRecord.Deleted, false, "note Deleted mismatch")
|
||||
assert.Equal(t, noteRecord.USN, expectedUSN, "note USN mismatch")
|
||||
assert.Equal(t, userRecord.MaxUSN, expectedUSN, "user MaxUSN mismatch")
|
||||
|
|
|
|||
|
|
@ -222,7 +222,7 @@ func (n *Notes) create(r *http.Request) (database.Note, error) {
|
|||
}
|
||||
|
||||
client := getClientType(r)
|
||||
note, err := n.app.CreateNote(*user, params.BookUUID, params.Content, params.AddedOn, params.EditedOn, false, client)
|
||||
note, err := n.app.CreateNote(*user, params.BookUUID, params.Content, params.AddedOn, params.EditedOn, client)
|
||||
if err != nil {
|
||||
return database.Note{}, errors.Wrap(err, "creating note")
|
||||
}
|
||||
|
|
@ -301,11 +301,10 @@ func (n *Notes) V3Delete(w http.ResponseWriter, r *http.Request) {
|
|||
type updateNotePayload struct {
|
||||
BookUUID *string `schema:"book_uuid" json:"book_uuid"`
|
||||
Content *string `schema:"content" json:"content"`
|
||||
Public *bool `schema:"public" json:"public"`
|
||||
}
|
||||
|
||||
func validateUpdateNotePayload(p updateNotePayload) error {
|
||||
if p.BookUUID == nil && p.Content == nil && p.Public == nil {
|
||||
if p.BookUUID == nil && p.Content == nil {
|
||||
return app.ErrEmptyUpdate
|
||||
}
|
||||
|
||||
|
|
@ -341,7 +340,6 @@ func (n *Notes) update(r *http.Request) (database.Note, error) {
|
|||
note, err = n.app.UpdateNote(tx, *user, note, &app.UpdateNoteParams{
|
||||
BookUUID: params.BookUUID,
|
||||
Content: params.Content,
|
||||
Public: params.Public,
|
||||
})
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
|
|
|
|||
|
|
@ -42,7 +42,6 @@ func getExpectedNotePayload(n database.Note, b database.Book, u database.User) p
|
|||
UpdatedAt: truncateMicro(n.UpdatedAt),
|
||||
Body: n.Body,
|
||||
AddedOn: n.AddedOn,
|
||||
Public: n.Public,
|
||||
USN: n.USN,
|
||||
Book: presenters.NoteBook{
|
||||
UUID: b.UUID,
|
||||
|
|
@ -189,7 +188,9 @@ func TestGetNote(t *testing.T) {
|
|||
defer server.Close()
|
||||
|
||||
user := testutils.SetupUserData(db)
|
||||
testutils.SetupAccountData(db, user, "user@test.com", "pass1234")
|
||||
anotherUser := testutils.SetupUserData(db)
|
||||
testutils.SetupAccountData(db, anotherUser, "another@test.com", "pass1234")
|
||||
|
||||
b1 := database.Book{
|
||||
UUID: testutils.MustUUID(t),
|
||||
|
|
@ -198,22 +199,13 @@ func TestGetNote(t *testing.T) {
|
|||
}
|
||||
testutils.MustExec(t, db.Save(&b1), "preparing b1")
|
||||
|
||||
privateNote := database.Note{
|
||||
note := database.Note{
|
||||
UUID: testutils.MustUUID(t),
|
||||
UserID: user.ID,
|
||||
BookUUID: b1.UUID,
|
||||
Body: "privateNote content",
|
||||
Public: false,
|
||||
Body: "note content",
|
||||
}
|
||||
testutils.MustExec(t, db.Save(&privateNote), "preparing privateNote")
|
||||
publicNote := database.Note{
|
||||
UUID: testutils.MustUUID(t),
|
||||
UserID: user.ID,
|
||||
BookUUID: b1.UUID,
|
||||
Body: "publicNote content",
|
||||
Public: true,
|
||||
}
|
||||
testutils.MustExec(t, db.Save(&publicNote), "preparing publicNote")
|
||||
testutils.MustExec(t, db.Save(¬e), "preparing note")
|
||||
deletedNote := database.Note{
|
||||
UUID: testutils.MustUUID(t),
|
||||
UserID: user.ID,
|
||||
|
|
@ -226,9 +218,9 @@ func TestGetNote(t *testing.T) {
|
|||
return fmt.Sprintf("/api/v3/notes/%s", noteUUID)
|
||||
}
|
||||
|
||||
t.Run("owner accessing private note", func(t *testing.T) {
|
||||
t.Run("owner accessing note", func(t *testing.T) {
|
||||
// Execute
|
||||
url := getURL(publicNote.UUID)
|
||||
url := getURL(note.UUID)
|
||||
req := testutils.MakeReq(server.URL, "GET", url, "")
|
||||
res := testutils.HTTPAuthDo(t, db, req, user)
|
||||
|
||||
|
|
@ -240,58 +232,16 @@ func TestGetNote(t *testing.T) {
|
|||
t.Fatal(errors.Wrap(err, "decoding payload"))
|
||||
}
|
||||
|
||||
var n2Record database.Note
|
||||
testutils.MustExec(t, db.Where("uuid = ?", publicNote.UUID).First(&n2Record), "finding n2Record")
|
||||
var noteRecord database.Note
|
||||
testutils.MustExec(t, db.Where("uuid = ?", note.UUID).First(¬eRecord), "finding noteRecord")
|
||||
|
||||
expected := getExpectedNotePayload(n2Record, b1, user)
|
||||
expected := getExpectedNotePayload(noteRecord, b1, user)
|
||||
assert.DeepEqual(t, payload, expected, "payload mismatch")
|
||||
})
|
||||
|
||||
t.Run("owner accessing public note", func(t *testing.T) {
|
||||
t.Run("non-owner accessing note", func(t *testing.T) {
|
||||
// Execute
|
||||
url := getURL(publicNote.UUID)
|
||||
req := testutils.MakeReq(server.URL, "GET", url, "")
|
||||
res := testutils.HTTPAuthDo(t, db, req, user)
|
||||
|
||||
// Test
|
||||
assert.StatusCodeEquals(t, res, http.StatusOK, "")
|
||||
|
||||
var payload presenters.Note
|
||||
if err := json.NewDecoder(res.Body).Decode(&payload); err != nil {
|
||||
t.Fatal(errors.Wrap(err, "decoding payload"))
|
||||
}
|
||||
|
||||
var n2Record database.Note
|
||||
testutils.MustExec(t, db.Where("uuid = ?", publicNote.UUID).First(&n2Record), "finding n2Record")
|
||||
|
||||
expected := getExpectedNotePayload(n2Record, b1, user)
|
||||
assert.DeepEqual(t, payload, expected, "payload mismatch")
|
||||
})
|
||||
|
||||
t.Run("non-owner accessing public note", func(t *testing.T) {
|
||||
// Execute
|
||||
url := getURL(publicNote.UUID)
|
||||
req := testutils.MakeReq(server.URL, "GET", url, "")
|
||||
res := testutils.HTTPAuthDo(t, db, req, anotherUser)
|
||||
|
||||
// Test
|
||||
assert.StatusCodeEquals(t, res, http.StatusOK, "")
|
||||
|
||||
var payload presenters.Note
|
||||
if err := json.NewDecoder(res.Body).Decode(&payload); err != nil {
|
||||
t.Fatal(errors.Wrap(err, "decoding payload"))
|
||||
}
|
||||
|
||||
var n2Record database.Note
|
||||
testutils.MustExec(t, db.Where("uuid = ?", publicNote.UUID).First(&n2Record), "finding n2Record")
|
||||
|
||||
expected := getExpectedNotePayload(n2Record, b1, user)
|
||||
assert.DeepEqual(t, payload, expected, "payload mismatch")
|
||||
})
|
||||
|
||||
t.Run("non-owner accessing private note", func(t *testing.T) {
|
||||
// Execute
|
||||
url := getURL(privateNote.UUID)
|
||||
url := getURL(note.UUID)
|
||||
req := testutils.MakeReq(server.URL, "GET", url, "")
|
||||
res := testutils.HTTPAuthDo(t, db, req, anotherUser)
|
||||
|
||||
|
|
@ -306,42 +256,21 @@ func TestGetNote(t *testing.T) {
|
|||
assert.DeepEqual(t, string(body), "not found\n", "payload mismatch")
|
||||
})
|
||||
|
||||
t.Run("guest accessing public note", func(t *testing.T) {
|
||||
t.Run("guest accessing note", func(t *testing.T) {
|
||||
// Execute
|
||||
url := getURL(publicNote.UUID)
|
||||
url := getURL(note.UUID)
|
||||
req := testutils.MakeReq(server.URL, "GET", url, "")
|
||||
res := testutils.HTTPDo(t, req)
|
||||
|
||||
// Test
|
||||
assert.StatusCodeEquals(t, res, http.StatusOK, "")
|
||||
|
||||
var payload presenters.Note
|
||||
if err := json.NewDecoder(res.Body).Decode(&payload); err != nil {
|
||||
t.Fatal(errors.Wrap(err, "decoding payload"))
|
||||
}
|
||||
|
||||
var n2Record database.Note
|
||||
testutils.MustExec(t, db.Where("uuid = ?", publicNote.UUID).First(&n2Record), "finding n2Record")
|
||||
|
||||
expected := getExpectedNotePayload(n2Record, b1, user)
|
||||
assert.DeepEqual(t, payload, expected, "payload mismatch")
|
||||
})
|
||||
|
||||
t.Run("guest accessing private note", func(t *testing.T) {
|
||||
// Execute
|
||||
url := getURL(privateNote.UUID)
|
||||
req := testutils.MakeReq(server.URL, "GET", url, "")
|
||||
res := testutils.HTTPDo(t, req)
|
||||
|
||||
// Test
|
||||
assert.StatusCodeEquals(t, res, http.StatusNotFound, "")
|
||||
assert.StatusCodeEquals(t, res, http.StatusUnauthorized, "")
|
||||
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
t.Fatal(errors.Wrap(err, "reading body"))
|
||||
}
|
||||
|
||||
assert.DeepEqual(t, string(body), "not found\n", "payload mismatch")
|
||||
assert.DeepEqual(t, string(body), "unauthorized\n", "payload mismatch")
|
||||
})
|
||||
|
||||
t.Run("nonexistent", func(t *testing.T) {
|
||||
|
|
@ -533,7 +462,6 @@ func TestUpdateNote(t *testing.T) {
|
|||
type payloadData struct {
|
||||
Content *string `schema:"content" json:"content,omitempty"`
|
||||
BookUUID *string `schema:"book_uuid" json:"book_uuid,omitempty"`
|
||||
Public *bool `schema:"public" json:"public,omitempty"`
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
|
|
@ -541,12 +469,10 @@ func TestUpdateNote(t *testing.T) {
|
|||
noteUUID string
|
||||
noteBookUUID string
|
||||
noteBody string
|
||||
notePublic bool
|
||||
noteDeleted bool
|
||||
expectedNoteBody string
|
||||
expectedNoteBookName string
|
||||
expectedNoteBookUUID string
|
||||
expectedNotePublic bool
|
||||
}{
|
||||
{
|
||||
payload: testutils.PayloadWrapper{
|
||||
|
|
@ -556,13 +482,11 @@ func TestUpdateNote(t *testing.T) {
|
|||
},
|
||||
noteUUID: "ab50aa32-b232-40d8-b10f-10a7f9134053",
|
||||
noteBookUUID: b1UUID,
|
||||
notePublic: false,
|
||||
noteBody: "original content",
|
||||
noteDeleted: false,
|
||||
expectedNoteBookUUID: b1UUID,
|
||||
expectedNoteBody: "some updated content",
|
||||
expectedNoteBookName: "css",
|
||||
expectedNotePublic: false,
|
||||
},
|
||||
{
|
||||
payload: testutils.PayloadWrapper{
|
||||
|
|
@ -572,13 +496,11 @@ func TestUpdateNote(t *testing.T) {
|
|||
},
|
||||
noteUUID: "ab50aa32-b232-40d8-b10f-10a7f9134053",
|
||||
noteBookUUID: b1UUID,
|
||||
notePublic: false,
|
||||
noteBody: "original content",
|
||||
noteDeleted: false,
|
||||
expectedNoteBookUUID: b1UUID,
|
||||
expectedNoteBody: "original content",
|
||||
expectedNoteBookName: "css",
|
||||
expectedNotePublic: false,
|
||||
},
|
||||
{
|
||||
payload: testutils.PayloadWrapper{
|
||||
|
|
@ -588,13 +510,11 @@ func TestUpdateNote(t *testing.T) {
|
|||
},
|
||||
noteUUID: "ab50aa32-b232-40d8-b10f-10a7f9134053",
|
||||
noteBookUUID: b1UUID,
|
||||
notePublic: false,
|
||||
noteBody: "original content",
|
||||
noteDeleted: false,
|
||||
expectedNoteBookUUID: b2UUID,
|
||||
expectedNoteBody: "original content",
|
||||
expectedNoteBookName: "js",
|
||||
expectedNotePublic: false,
|
||||
},
|
||||
{
|
||||
payload: testutils.PayloadWrapper{
|
||||
|
|
@ -605,13 +525,11 @@ func TestUpdateNote(t *testing.T) {
|
|||
},
|
||||
noteUUID: "ab50aa32-b232-40d8-b10f-10a7f9134053",
|
||||
noteBookUUID: b1UUID,
|
||||
notePublic: false,
|
||||
noteBody: "original content",
|
||||
noteDeleted: false,
|
||||
expectedNoteBookUUID: b2UUID,
|
||||
expectedNoteBody: "some updated content",
|
||||
expectedNoteBookName: "js",
|
||||
expectedNotePublic: false,
|
||||
},
|
||||
{
|
||||
payload: testutils.PayloadWrapper{
|
||||
|
|
@ -622,80 +540,11 @@ func TestUpdateNote(t *testing.T) {
|
|||
},
|
||||
noteUUID: "ab50aa32-b232-40d8-b10f-10a7f9134053",
|
||||
noteBookUUID: b1UUID,
|
||||
notePublic: false,
|
||||
noteBody: "",
|
||||
noteDeleted: true,
|
||||
expectedNoteBookUUID: b1UUID,
|
||||
expectedNoteBody: updatedBody,
|
||||
expectedNoteBookName: "js",
|
||||
expectedNotePublic: false,
|
||||
},
|
||||
{
|
||||
payload: testutils.PayloadWrapper{
|
||||
Data: payloadData{
|
||||
Public: &testutils.TrueVal,
|
||||
},
|
||||
},
|
||||
noteUUID: "ab50aa32-b232-40d8-b10f-10a7f9134053",
|
||||
noteBookUUID: b1UUID,
|
||||
notePublic: false,
|
||||
noteBody: "original content",
|
||||
noteDeleted: false,
|
||||
expectedNoteBookUUID: b1UUID,
|
||||
expectedNoteBody: "original content",
|
||||
expectedNoteBookName: "css",
|
||||
expectedNotePublic: true,
|
||||
},
|
||||
{
|
||||
payload: testutils.PayloadWrapper{
|
||||
Data: payloadData{
|
||||
Public: &testutils.FalseVal,
|
||||
},
|
||||
},
|
||||
noteUUID: "ab50aa32-b232-40d8-b10f-10a7f9134053",
|
||||
noteBookUUID: b1UUID,
|
||||
notePublic: true,
|
||||
noteBody: "original content",
|
||||
noteDeleted: false,
|
||||
expectedNoteBookUUID: b1UUID,
|
||||
expectedNoteBody: "original content",
|
||||
expectedNoteBookName: "css",
|
||||
expectedNotePublic: false,
|
||||
},
|
||||
{
|
||||
payload: testutils.PayloadWrapper{
|
||||
Data: payloadData{
|
||||
Content: &updatedBody,
|
||||
Public: &testutils.FalseVal,
|
||||
},
|
||||
},
|
||||
noteUUID: "ab50aa32-b232-40d8-b10f-10a7f9134053",
|
||||
noteBookUUID: b1UUID,
|
||||
notePublic: true,
|
||||
noteBody: "original content",
|
||||
noteDeleted: false,
|
||||
expectedNoteBookUUID: b1UUID,
|
||||
expectedNoteBody: updatedBody,
|
||||
expectedNoteBookName: "css",
|
||||
expectedNotePublic: false,
|
||||
},
|
||||
{
|
||||
payload: testutils.PayloadWrapper{
|
||||
Data: payloadData{
|
||||
BookUUID: &b2UUID,
|
||||
Content: &updatedBody,
|
||||
Public: &testutils.TrueVal,
|
||||
},
|
||||
},
|
||||
noteUUID: "ab50aa32-b232-40d8-b10f-10a7f9134053",
|
||||
noteBookUUID: b1UUID,
|
||||
notePublic: false,
|
||||
noteBody: "original content",
|
||||
noteDeleted: false,
|
||||
expectedNoteBookUUID: b2UUID,
|
||||
expectedNoteBody: updatedBody,
|
||||
expectedNoteBookName: "js",
|
||||
expectedNotePublic: true,
|
||||
},
|
||||
}
|
||||
|
||||
|
|
@ -734,7 +583,6 @@ func TestUpdateNote(t *testing.T) {
|
|||
BookUUID: tc.noteBookUUID,
|
||||
Body: tc.noteBody,
|
||||
Deleted: tc.noteDeleted,
|
||||
Public: tc.notePublic,
|
||||
}
|
||||
testutils.MustExec(t, db.Save(¬e), "preparing note")
|
||||
|
||||
|
|
@ -765,7 +613,6 @@ func TestUpdateNote(t *testing.T) {
|
|||
assert.Equal(t, noteRecord.UUID, tc.noteUUID, "note uuid mismatch for test case")
|
||||
assert.Equal(t, noteRecord.Body, tc.expectedNoteBody, "note content mismatch for test case")
|
||||
assert.Equal(t, noteRecord.BookUUID, tc.expectedNoteBookUUID, "note book_uuid mismatch for test case")
|
||||
assert.Equal(t, noteRecord.Public, tc.expectedNotePublic, "note public mismatch for test case")
|
||||
assert.Equal(t, noteRecord.USN, 102, "note usn mismatch for test case")
|
||||
|
||||
assert.Equal(t, userRecord.MaxUSN, 102, "user max_usn mismatch for test case")
|
||||
|
|
|
|||
|
|
@ -82,7 +82,7 @@ func NewAPIRoutes(a *app.App, c *Controllers) []Route {
|
|||
{"POST", "/v3/signout", c.Users.V3Logout, true},
|
||||
{"OPTIONS", "/v3/signout", c.Users.logoutOptions, true},
|
||||
{"GET", "/v3/notes", mw.Auth(a.DB, c.Notes.V3Index, nil), true},
|
||||
{"GET", "/v3/notes/{noteUUID}", c.Notes.V3Show, true},
|
||||
{"GET", "/v3/notes/{noteUUID}", mw.Auth(a.DB, c.Notes.V3Show, nil), true},
|
||||
{"POST", "/v3/notes", mw.Auth(a.DB, c.Notes.V3Create, nil), true},
|
||||
{"DELETE", "/v3/notes/{noteUUID}", mw.Auth(a.DB, c.Notes.V3Delete, nil), true},
|
||||
{"PATCH", "/v3/notes/{noteUUID}", mw.Auth(a.DB, c.Notes.V3Update, nil), true},
|
||||
|
|
|
|||
|
|
@ -75,7 +75,6 @@ type SyncFragNote struct {
|
|||
AddedOn int64 `json:"added_on"`
|
||||
EditedOn int64 `json:"edited_on"`
|
||||
Body string `json:"content"`
|
||||
Public bool `json:"public"`
|
||||
Deleted bool `json:"deleted"`
|
||||
}
|
||||
|
||||
|
|
@ -89,7 +88,6 @@ func NewFragNote(note database.Note) SyncFragNote {
|
|||
AddedOn: note.AddedOn,
|
||||
EditedOn: note.EditedOn,
|
||||
Body: note.Body,
|
||||
Public: note.Public,
|
||||
Deleted: note.Deleted,
|
||||
BookUUID: note.BookUUID,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -53,7 +53,6 @@ type Note struct {
|
|||
Body string `json:"content"`
|
||||
AddedOn int64 `json:"added_on"`
|
||||
EditedOn int64 `json:"edited_on"`
|
||||
Public bool `json:"public" gorm:"default:false"`
|
||||
USN int `json:"-" gorm:"index"`
|
||||
Deleted bool `json:"-" gorm:"default:false"`
|
||||
Client string `gorm:"index"`
|
||||
|
|
|
|||
|
|
@ -40,29 +40,17 @@ func TestGetNote(t *testing.T) {
|
|||
}
|
||||
testutils.MustExec(t, db.Save(&b1), "preparing b1")
|
||||
|
||||
privateNote := database.Note{
|
||||
note := database.Note{
|
||||
UUID: testutils.MustUUID(t),
|
||||
UserID: user.ID,
|
||||
BookUUID: b1.UUID,
|
||||
Body: "privateNote content",
|
||||
Body: "note content",
|
||||
Deleted: false,
|
||||
Public: false,
|
||||
}
|
||||
testutils.MustExec(t, db.Save(&privateNote), "preparing privateNote")
|
||||
testutils.MustExec(t, db.Save(¬e), "preparing note")
|
||||
|
||||
publicNote := database.Note{
|
||||
UUID: testutils.MustUUID(t),
|
||||
UserID: user.ID,
|
||||
BookUUID: b1.UUID,
|
||||
Body: "privateNote content",
|
||||
Deleted: false,
|
||||
Public: true,
|
||||
}
|
||||
testutils.MustExec(t, db.Save(&publicNote), "preparing privateNote")
|
||||
|
||||
var privateNoteRecord, publicNoteRecord database.Note
|
||||
testutils.MustExec(t, db.Where("uuid = ?", privateNote.UUID).Preload("Book").Preload("User").First(&privateNoteRecord), "finding privateNote")
|
||||
testutils.MustExec(t, db.Where("uuid = ?", publicNote.UUID).Preload("Book").Preload("User").First(&publicNoteRecord), "finding publicNote")
|
||||
var noteRecord database.Note
|
||||
testutils.MustExec(t, db.Where("uuid = ?", note.UUID).Preload("Book").Preload("User").First(¬eRecord), "finding note")
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
|
|
@ -72,40 +60,26 @@ func TestGetNote(t *testing.T) {
|
|||
expectedNote database.Note
|
||||
}{
|
||||
{
|
||||
name: "owner accessing private note",
|
||||
name: "owner accessing note",
|
||||
user: user,
|
||||
note: privateNote,
|
||||
note: note,
|
||||
expectedOK: true,
|
||||
expectedNote: privateNoteRecord,
|
||||
expectedNote: noteRecord,
|
||||
},
|
||||
{
|
||||
name: "non-owner accessing private note",
|
||||
name: "non-owner accessing note",
|
||||
user: anotherUser,
|
||||
note: privateNote,
|
||||
note: note,
|
||||
expectedOK: false,
|
||||
expectedNote: database.Note{},
|
||||
},
|
||||
{
|
||||
name: "non-owner accessing public note",
|
||||
user: anotherUser,
|
||||
note: publicNote,
|
||||
expectedOK: true,
|
||||
expectedNote: publicNoteRecord,
|
||||
},
|
||||
{
|
||||
name: "guest accessing private note",
|
||||
name: "guest accessing note",
|
||||
user: database.User{},
|
||||
note: privateNote,
|
||||
note: note,
|
||||
expectedOK: false,
|
||||
expectedNote: database.Note{},
|
||||
},
|
||||
{
|
||||
name: "guest accessing public note",
|
||||
user: database.User{},
|
||||
note: publicNote,
|
||||
expectedOK: true,
|
||||
expectedNote: publicNoteRecord,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
|
|
@ -139,7 +113,6 @@ func TestGetNote_nonexistent(t *testing.T) {
|
|||
BookUUID: b1.UUID,
|
||||
Body: "n1 content",
|
||||
Deleted: false,
|
||||
Public: false,
|
||||
}
|
||||
testutils.MustExec(t, db.Save(&n1), "preparing n1")
|
||||
|
||||
|
|
|
|||
|
|
@ -24,9 +24,6 @@ import (
|
|||
|
||||
// ViewNote checks if the given user can view the given note
|
||||
func ViewNote(user *database.User, note database.Note) bool {
|
||||
if note.Public {
|
||||
return true
|
||||
}
|
||||
if user == nil {
|
||||
return false
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,53 +39,27 @@ func TestViewNote(t *testing.T) {
|
|||
}
|
||||
testutils.MustExec(t, db.Save(&b1), "preparing b1")
|
||||
|
||||
privateNote := database.Note{
|
||||
note := database.Note{
|
||||
UUID: testutils.MustUUID(t),
|
||||
UserID: user.ID,
|
||||
BookUUID: b1.UUID,
|
||||
Body: "privateNote content",
|
||||
Body: "note content",
|
||||
Deleted: false,
|
||||
Public: false,
|
||||
}
|
||||
testutils.MustExec(t, db.Save(&privateNote), "preparing privateNote")
|
||||
testutils.MustExec(t, db.Save(¬e), "preparing note")
|
||||
|
||||
publicNote := database.Note{
|
||||
UUID: testutils.MustUUID(t),
|
||||
UserID: user.ID,
|
||||
BookUUID: b1.UUID,
|
||||
Body: "privateNote content",
|
||||
Deleted: false,
|
||||
Public: true,
|
||||
}
|
||||
testutils.MustExec(t, db.Save(&publicNote), "preparing privateNote")
|
||||
|
||||
t.Run("owner accessing private note", func(t *testing.T) {
|
||||
result := ViewNote(&user, privateNote)
|
||||
t.Run("owner accessing note", func(t *testing.T) {
|
||||
result := ViewNote(&user, note)
|
||||
assert.Equal(t, result, true, "result mismatch")
|
||||
})
|
||||
|
||||
t.Run("owner accessing public note", func(t *testing.T) {
|
||||
result := ViewNote(&user, publicNote)
|
||||
assert.Equal(t, result, true, "result mismatch")
|
||||
})
|
||||
|
||||
t.Run("non-owner accessing private note", func(t *testing.T) {
|
||||
result := ViewNote(&anotherUser, privateNote)
|
||||
t.Run("non-owner accessing note", func(t *testing.T) {
|
||||
result := ViewNote(&anotherUser, note)
|
||||
assert.Equal(t, result, false, "result mismatch")
|
||||
})
|
||||
|
||||
t.Run("non-owner accessing public note", func(t *testing.T) {
|
||||
result := ViewNote(&anotherUser, publicNote)
|
||||
assert.Equal(t, result, true, "result mismatch")
|
||||
})
|
||||
|
||||
t.Run("guest accessing private note", func(t *testing.T) {
|
||||
result := ViewNote(nil, privateNote)
|
||||
t.Run("guest accessing note", func(t *testing.T) {
|
||||
result := ViewNote(nil, note)
|
||||
assert.Equal(t, result, false, "result mismatch")
|
||||
})
|
||||
|
||||
t.Run("guest accessing public note", func(t *testing.T) {
|
||||
result := ViewNote(nil, publicNote)
|
||||
assert.Equal(t, result, true, "result mismatch")
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,7 +31,6 @@ type Note struct {
|
|||
UpdatedAt time.Time `json:"updated_at"`
|
||||
Body string `json:"content"`
|
||||
AddedOn int64 `json:"added_on"`
|
||||
Public bool `json:"public"`
|
||||
USN int `json:"usn"`
|
||||
Book NoteBook `json:"book"`
|
||||
User NoteUser `json:"user"`
|
||||
|
|
@ -57,7 +56,6 @@ func PresentNote(note database.Note) Note {
|
|||
UpdatedAt: FormatTS(note.UpdatedAt),
|
||||
Body: note.Body,
|
||||
AddedOn: note.AddedOn,
|
||||
Public: note.Public,
|
||||
USN: note.USN,
|
||||
Book: NoteBook{
|
||||
UUID: note.Book.UUID,
|
||||
|
|
|
|||
|
|
@ -41,7 +41,6 @@ func TestPresentNote(t *testing.T) {
|
|||
BookUUID: "f1e2d3c4-b5a6-4987-b654-321fedcba098",
|
||||
Body: "Test note content",
|
||||
AddedOn: 1234567890,
|
||||
Public: true,
|
||||
USN: 100,
|
||||
Book: database.Book{
|
||||
UUID: "f1e2d3c4-b5a6-4987-b654-321fedcba098",
|
||||
|
|
@ -57,7 +56,6 @@ func TestPresentNote(t *testing.T) {
|
|||
assert.Equal(t, got.UUID, "a1b2c3d4-e5f6-4789-a012-3456789abcde", "UUID mismatch")
|
||||
assert.Equal(t, got.Body, "Test note content", "Body mismatch")
|
||||
assert.Equal(t, got.AddedOn, int64(1234567890), "AddedOn mismatch")
|
||||
assert.Equal(t, got.Public, true, "Public mismatch")
|
||||
assert.Equal(t, got.USN, 100, "USN mismatch")
|
||||
assert.Equal(t, got.CreatedAt, FormatTS(createdAt), "CreatedAt mismatch")
|
||||
assert.Equal(t, got.UpdatedAt, FormatTS(updatedAt), "UpdatedAt mismatch")
|
||||
|
|
@ -84,7 +82,6 @@ func TestPresentNotes(t *testing.T) {
|
|||
BookUUID: "f1e2d3c4-b5a6-4987-b654-321fedcba098",
|
||||
Body: "First note",
|
||||
AddedOn: 1000000000,
|
||||
Public: false,
|
||||
USN: 10,
|
||||
Book: database.Book{
|
||||
UUID: "f1e2d3c4-b5a6-4987-b654-321fedcba098",
|
||||
|
|
@ -105,7 +102,6 @@ func TestPresentNotes(t *testing.T) {
|
|||
BookUUID: "abcdef01-2345-4678-9abc-def012345678",
|
||||
Body: "Second note",
|
||||
AddedOn: 2000000000,
|
||||
Public: true,
|
||||
USN: 20,
|
||||
Book: database.Book{
|
||||
UUID: "abcdef01-2345-4678-9abc-def012345678",
|
||||
|
|
|
|||
|
|
@ -19,12 +19,10 @@
|
|||
package tmpl
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/dnote/dnote/pkg/assert"
|
||||
"github.com/dnote/dnote/pkg/server/database"
|
||||
"github.com/dnote/dnote/pkg/server/testutils"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
|
@ -50,42 +48,4 @@ func TestAppShellExecute(t *testing.T) {
|
|||
|
||||
assert.Equal(t, string(b), "<head><title>Dnote</title></head>", "result mismatch")
|
||||
})
|
||||
|
||||
t.Run("note", func(t *testing.T) {
|
||||
db := testutils.InitMemoryDB(t)
|
||||
|
||||
user := testutils.SetupUserData(db)
|
||||
b1 := database.Book{
|
||||
UUID: testutils.MustUUID(t),
|
||||
UserID: user.ID,
|
||||
Label: "js",
|
||||
}
|
||||
testutils.MustExec(t, db.Save(&b1), "preparing b1")
|
||||
n1 := database.Note{
|
||||
UUID: testutils.MustUUID(t),
|
||||
UserID: user.ID,
|
||||
BookUUID: b1.UUID,
|
||||
Public: true,
|
||||
Body: "n1 content",
|
||||
}
|
||||
testutils.MustExec(t, db.Save(&n1), "preparing note")
|
||||
|
||||
a, err := NewAppShell(db, []byte("{{ .MetaTags }}"))
|
||||
if err != nil {
|
||||
t.Fatal(errors.Wrap(err, "preparing app shell"))
|
||||
}
|
||||
|
||||
endpoint := fmt.Sprintf("http://mock.url/notes/%s", n1.UUID)
|
||||
r, err := http.NewRequest("GET", endpoint, nil)
|
||||
if err != nil {
|
||||
t.Fatal(errors.Wrap(err, "preparing request"))
|
||||
}
|
||||
|
||||
b, err := a.Execute(r)
|
||||
if err != nil {
|
||||
t.Fatal(errors.Wrap(err, "executing"))
|
||||
}
|
||||
|
||||
assert.NotEqual(t, string(b), "", "result should not be empty")
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue