-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconcurrent_edit_test.go
More file actions
52 lines (46 loc) · 1.63 KB
/
concurrent_edit_test.go
File metadata and controls
52 lines (46 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package gobookmarks
import (
"context"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
func TestBookmarksEditSaveActionConcurrent(t *testing.T) {
p, user, _, ctx := setupCategoryEditTest(t)
original := "Category: A\nhttp://one.com one"
if err := p.CreateBookmarks(context.Background(), user, nil, "main", original); err != nil {
t.Fatalf("CreateBookmarks: %v", err)
}
_, sha1, err := p.GetBookmarks(context.Background(), user, "refs/heads/main", nil)
if err != nil {
t.Fatalf("GetBookmarks sha1: %v", err)
}
updated := "Category: B\nhttp://two.com two"
if err := p.UpdateBookmarks(context.Background(), user, nil, "refs/heads/main", "main", updated, sha1); err != nil {
t.Fatalf("UpdateBookmarks: %v", err)
}
_, sha2, err := p.GetBookmarks(context.Background(), user, "refs/heads/main", nil)
if err != nil {
t.Fatalf("GetBookmarks sha2: %v", err)
}
if sha1 == sha2 {
t.Fatalf("SHA did not change")
}
form := url.Values{"text": {"Category: C\nhttp://three.com three"}, "branch": {"main"}, "ref": {"refs/heads/main"}, "sha": {sha1}}
req := httptest.NewRequest("POST", "/edit/save", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req = req.WithContext(ctx)
w := httptest.NewRecorder()
err = BookmarksEditSaveAction(w, req)
if err == nil || !strings.Contains(err.Error(), "concurrently") {
t.Fatalf("expected concurrency error, got %v", err)
}
got, _, err := p.GetBookmarks(context.Background(), user, "refs/heads/main", nil)
if err != nil {
t.Fatalf("GetBookmarks final: %v", err)
}
if got != updated {
t.Fatalf("bookmarks changed unexpectedly: %q", got)
}
}