-
Notifications
You must be signed in to change notification settings - Fork 26
/
indieAuth_test.go
68 lines (53 loc) · 1.67 KB
/
indieAuth_test.go
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"go.hacdias.com/indielib/indieauth"
)
func Test_checkIndieAuth(t *testing.T) {
app := &goBlog{
httpClient: newFakeHttpClient().Client,
cfg: createDefaultTestConfig(t),
}
_ = app.initConfig(false)
app.initMarkdown()
app.initSessions()
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
checked1 := false
app.checkIndieAuth(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
checked1 = true
})).ServeHTTP(rec, req)
assert.False(t, checked1)
token, err := app.db.indieAuthSaveToken(&indieauth.AuthenticationRequest{
ClientID: "https://example.com/",
Scopes: strings.Split("create update delete", " "),
})
assert.NoError(t, err)
assert.NotEmpty(t, token)
req.Header.Set("Authorization", "Bearer "+token)
checked2 := false
app.checkIndieAuth(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
assert.Equal(t, "create update delete", r.Context().Value(indieAuthScope).(string))
checked2 = true
})).ServeHTTP(rec, req)
assert.True(t, checked2)
}
func Test_addAllScopes(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
checked := false
addAllScopes(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
scope := r.Context().Value(indieAuthScope).(string)
assert.Contains(t, scope, "create")
assert.Contains(t, scope, "update")
assert.Contains(t, scope, "delete")
assert.Contains(t, scope, "undelete")
assert.Contains(t, scope, "media")
checked = true
})).ServeHTTP(rec, req)
assert.True(t, checked)
}