Browse Source

note removal implemented

master
Christian Müller 8 years ago
parent
commit
204d4463c9
  1. 44
      api_spec.js
  2. 5
      server.go
  3. 15
      storage.go

44
api_spec.js

@ -109,7 +109,7 @@ frisby.create('Valid posting')
}) })
.toss(); .toss();
frisby.create('Valid posting, editing and more') frisby.create('Valid posting, editing and removal')
.post('http://localhost:3000/note', { .post('http://localhost:3000/note', {
password: 'aabbcc', password: 'aabbcc',
tos: 'on', tos: 'on',
@ -157,20 +157,16 @@ frisby.create('Valid posting, editing and more')
.get('http://localhost:3000/' + noteId) .get('http://localhost:3000/' + noteId)
.expectStatus(200) .expectStatus(200)
.expectBodyContains('Changed text!') .expectBodyContains('Changed text!')
.toss(); .after((err, res, body) => {
})
.toss();
})
.toss();
frisby.create('Read export of posted note') frisby.create('Read export of posted note')
.expectStatus(200) .expectStatus(200)
.get('http://localhost:3000/' + noteId + '/export') .get('http://localhost:3000/' + noteId + '/export')
.expectHeaderContains('content-type', 'text/plain; charset=utf-8') .expectHeaderContains('content-type', 'text/plain; charset=utf-8')
.expectBodyContains(testNote) .expectBodyContains('Changed text!')
.toss(); .toss();
frisby.create('Open /edit on posted note') frisby.create('Open /edit on posted note')
.expectStatus(200) .expectStatus(200)
.expectBodyContains('<textarea autofocus name="text">' + testNote + '</textarea>') .expectBodyContains('<textarea autofocus name="text">Changed text!</textarea>')
.get('http://localhost:3000/' + noteId + '/edit') .get('http://localhost:3000/' + noteId + '/edit')
.toss(); .toss();
frisby.create('Read stats of posted note') frisby.create('Read stats of posted note')
@ -181,6 +177,38 @@ frisby.create('Valid posting, editing and more')
.expectBodyContains('<tr><td>Views</td><td>0</td></tr>') .expectBodyContains('<tr><td>Views</td><td>0</td></tr>')
.toss(); .toss();
}) })
.after((err, res, body) => {
frisby.create('Note available')
.get('http://localhost:3000/' + noteId)
.expectStatus(200)
.toss();
frisby.create('Delete note with empty password')
.post('http://localhost:3000/note', { "id": noteId, "tos": "on", "text": "" })
.expectStatus(400)
.toss();
frisby.create('Delete note with wrong password')
.post('http://localhost:3000/note', { "id": noteId, "tos": "on", "text": "", "password": "xxyycc" })
.expectStatus(401)
.toss();
})
.after((err, res, body) => {
frisby.create('Delete note')
.post('http://localhost:3000/note', { "id": noteId, "tos": "on", "text": "", "password": "aabbcc" })
.expectStatus(301)
.after(function(err, res, body) {
frisby.create('Note unavailable')
.get('http://localhost:3000/' + noteId)
.expectStatus(404)
.toss();
})
.toss();
})
.toss();
})
.toss();
})
.toss();
})
.toss(); .toss();
var tooLongNote = 'ABCD'; var tooLongNote = 'ABCD';

5
server.go

@ -122,14 +122,15 @@ func main() {
c.Logger().Errorf("POST /note error: %d", code) c.Logger().Errorf("POST /note error: %d", code)
return c.Render(code, "Note", responsePage(code)) return c.Render(code, "Note", responsePage(code))
} }
id := c.FormValue("id")
text := c.FormValue("text") text := c.FormValue("text")
if 10 > len(text) || len(text) > 50000 { l := len(text)
if (id == "" || id != "" && l != 0) && (10 > l || l > 50000) {
code := http.StatusBadRequest code := http.StatusBadRequest
c.Logger().Errorf("POST /note error: %d", code) c.Logger().Errorf("POST /note error: %d", code)
return c.Render(code, "Note", return c.Render(code, "Note",
responsePage(code, "note length not accepted")) responsePage(code, "note length not accepted"))
} }
id := c.FormValue("id")
n := &Note{ n := &Note{
ID: id, ID: id,
Text: text, Text: text,

15
storage.go

@ -54,9 +54,18 @@ func update(c echo.Context, db *sql.DB, n *Note) (*Note, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
stmt, _ := tx.Prepare("update notes set (text, edited, password) = (?, ?, ?) where id = ? and (password = ? or password = ?)") s := "update notes set (text, edited, password) = (?, ?, ?) where id = ? and (password = ? or password = ?)"
if n.Text == "" {
s = "delete from notes where id = ? and (password = ? or password = ?)"
}
stmt, _ := tx.Prepare(s)
defer stmt.Close() defer stmt.Close()
res, err := stmt.Exec(n.Text, time.Now(), n.Password, n.ID, n.Password, n.DeprecatedPassword) var res sql.Result
if n.Text == "" {
res, err = stmt.Exec(n.ID, n.Password, n.DeprecatedPassword)
} else {
res, err = stmt.Exec(n.Text, time.Now(), n.Password, n.ID, n.Password, n.DeprecatedPassword)
}
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
return nil, err return nil, err
@ -66,7 +75,7 @@ func update(c echo.Context, db *sql.DB, n *Note) (*Note, error) {
tx.Rollback() tx.Rollback()
return nil, errorUnathorised return nil, errorUnathorised
} }
c.Logger().Debugf("updating note %s; committing transaction", n.ID) c.Logger().Debugf("updating note %s (deletion: %t); committing transaction", n.ID, n.Text == "")
return n, tx.Commit() return n, tx.Commit()
} }

Loading…
Cancel
Save