1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package security
16
17 import (
18 "context"
19 "net/http"
20 "testing"
21
22 "github.com/stretchr/testify/assert"
23 "github.com/stretchr/testify/require"
24 )
25
26 func TestAuthorized(t *testing.T) {
27 authorizer := Authorized()
28
29 err := authorizer.Authorize(nil, nil)
30 require.NoError(t, err)
31 }
32
33 func TestAuthenticator(t *testing.T) {
34 r, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "/", nil)
35 require.NoError(t, err)
36
37 t.Run("with HttpAuthenticator", func(t *testing.T) {
38 auth := HttpAuthenticator(func(_ *http.Request) (bool, interface{}, error) { return true, "test", nil })
39
40 t.Run("authenticator should work on *http.Request", func(t *testing.T) {
41 isAuth, user, err := auth.Authenticate(r)
42 require.NoError(t, err)
43 assert.True(t, isAuth)
44 assert.Equal(t, "test", user)
45 })
46
47 t.Run("authenticator should work on *ScopedAuthRequest", func(t *testing.T) {
48 scoped := &ScopedAuthRequest{Request: r}
49
50 isAuth, user, err := auth.Authenticate(scoped)
51 require.NoError(t, err)
52 assert.True(t, isAuth)
53 assert.Equal(t, "test", user)
54 })
55
56 t.Run("authenticator should return false on other inputs", func(t *testing.T) {
57 isAuth, user, err := auth.Authenticate("")
58 require.NoError(t, err)
59 assert.False(t, isAuth)
60 assert.Empty(t, user)
61 })
62 })
63
64 t.Run("with ScopedAuthenticator", func(t *testing.T) {
65 auth := ScopedAuthenticator(func(_ *ScopedAuthRequest) (bool, interface{}, error) { return true, "test", nil })
66
67 t.Run("authenticator should work on *ScopedAuthRequest", func(t *testing.T) {
68 scoped := &ScopedAuthRequest{Request: r}
69
70 isAuth, user, err := auth.Authenticate(scoped)
71 require.NoError(t, err)
72 assert.True(t, isAuth)
73 assert.Equal(t, "test", user)
74 })
75
76 t.Run("authenticator should return false on other inputs", func(t *testing.T) {
77 isAuth, user, err := auth.Authenticate("")
78 require.NoError(t, err)
79 assert.False(t, isAuth)
80 assert.Empty(t, user)
81 })
82 })
83 }
84
View as plain text