-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also, fix a lint. Fixes #116 Signed-off-by: Chris Palmer <[email protected]>
- Loading branch information
Chris Palmer
committed
Apr 2, 2024
1 parent
6c79b50
commit 4394028
Showing
6 changed files
with
89 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,12 +129,19 @@ func TestServeSave(t *testing.T) { | |
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
db.Save(&Link{Short: "link-owned-by-tagged-devices", Long: "/before", Owner: "tagged-devices"}) | ||
|
||
fooXSRF := func(short string) string { | ||
return xsrftoken.Generate(xsrfKey, "[email protected]", short) | ||
} | ||
barXSRF := func(short string) string { | ||
return xsrftoken.Generate(xsrfKey, "[email protected]", short) | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
short string | ||
xsrf string | ||
long string | ||
allowUnknownUsers bool | ||
currentUser func(*http.Request) (user, error) | ||
|
@@ -155,33 +162,38 @@ func TestServeSave(t *testing.T) { | |
{ | ||
name: "save simple link", | ||
short: "who", | ||
xsrf: fooXSRF(newShortName), | ||
long: "http://who/", | ||
wantStatus: http.StatusOK, | ||
}, | ||
{ | ||
name: "disallow editing another's link", | ||
short: "who", | ||
xsrf: barXSRF("who"), | ||
long: "http://who/", | ||
currentUser: func(*http.Request) (user, error) { return user{login: "[email protected]"}, nil }, | ||
wantStatus: http.StatusForbidden, | ||
}, | ||
{ | ||
name: "allow editing link owned by tagged-devices", | ||
short: "link-owned-by-tagged-devices", | ||
xsrf: barXSRF("link-owned-by-tagged-devices"), | ||
long: "/after", | ||
currentUser: func(*http.Request) (user, error) { return user{login: "[email protected]"}, nil }, | ||
wantStatus: http.StatusOK, | ||
}, | ||
{ | ||
name: "admins can edit any link", | ||
short: "who", | ||
xsrf: barXSRF("who"), | ||
long: "http://who/", | ||
currentUser: func(*http.Request) (user, error) { return user{login: "[email protected]", isAdmin: true}, nil }, | ||
wantStatus: http.StatusOK, | ||
}, | ||
{ | ||
name: "disallow unknown users", | ||
short: "who2", | ||
xsrf: fooXSRF("who2"), | ||
long: "http://who/", | ||
currentUser: func(*http.Request) (user, error) { return user{}, errors.New("") }, | ||
wantStatus: http.StatusInternalServerError, | ||
|
@@ -194,6 +206,13 @@ func TestServeSave(t *testing.T) { | |
currentUser: func(*http.Request) (user, error) { return user{}, nil }, | ||
wantStatus: http.StatusOK, | ||
}, | ||
{ | ||
name: "invalid xsrf", | ||
short: "goat", | ||
xsrf: fooXSRF("sheep"), | ||
long: "https://goat.example.com/goat.php?goat=true", | ||
wantStatus: http.StatusBadRequest, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
|
@@ -213,6 +232,7 @@ func TestServeSave(t *testing.T) { | |
r := httptest.NewRequest("POST", "/", strings.NewReader(url.Values{ | ||
"short": {tt.short}, | ||
"long": {tt.long}, | ||
"xsrf": {tt.xsrf}, | ||
}.Encode())) | ||
r.Header.Set("Content-Type", "application/x-www-form-urlencoded") | ||
w := httptest.NewRecorder() | ||
|
@@ -252,7 +272,7 @@ func TestServeDelete(t *testing.T) { | |
wantStatus: http.StatusBadRequest, | ||
}, | ||
{ | ||
name: "non-existant link", | ||
name: "nonexistent link", | ||
short: "does-not-exist", | ||
wantStatus: http.StatusNotFound, | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -122,7 +122,7 @@ <h2 id="api">Application Programming Interface (API)</h2> | |
<p> | ||
Create a new link by sending a POST request with a <code>short</code> and <code>long</code> value: | ||
|
||
<pre>{{`$ curl -L -d short=cs -d long=https://cs.github.com/ go | ||
<pre>{{`$ curl -L -H Sec-Golink:1 -d short=cs -d long=https://cs.github.com/ go | ||
{"Short":"cs","Long":"https://cs.github.com/","Created":"2022-06-03T22:15:29.993978392Z","LastEdit":"2022-06-03T22:15:29.993978392Z","Owner":"[email protected]"}`}} | ||
</pre> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters