Skip to content

Commit 2042ddd

Browse files
committed
test: add test case to test AuthChainOptions our main API
doc: a bit of godoc Signed-off-by: Sandor Szücs <sandor.szuecs@zalando.de>
1 parent 71d6d37 commit 2042ddd

File tree

4 files changed

+97
-1
lines changed

4 files changed

+97
-1
lines changed

ginoauth2.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ func Auth(accessCheckFunction AccessCheckFunction, endpoints oauth2.Endpoint) gi
274274
// }
275275
// var acl []ginoauth2.AccessTuple = []ginoauth2.AccessTuple{{"employee", 1070, "sszuecs"}, {"employee", 1114, "njuettner"}}
276276
// router := gin.Default()
277-
// private := router.Group("")
277+
// private := router.Group("")
278278
// checkChain := []AccessCheckFunction{
279279
// ginoauth2.UidCheck,
280280
// ginoauth2.GroupCheck,
@@ -287,6 +287,7 @@ func AuthChain(endpoint oauth2.Endpoint, accessCheckFunctions ...AccessCheckFunc
287287
return AuthChainOptions(Options{Endpoint: endpoint}, accessCheckFunctions...)
288288
}
289289

290+
// AuthChainOptions
290291
func AuthChainOptions(o Options, accessCheckFunctions ...AccessCheckFunction) gin.HandlerFunc {
291292
// init
292293
AuthInfoURL = o.Endpoint.TokenURL

ginoauth2_test.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package ginoauth2_test
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"maps"
7+
"net/http"
8+
"net/http/httptest"
9+
"os"
10+
"testing"
11+
12+
"github.com/gin-gonic/gin"
13+
"github.com/sirupsen/logrus"
14+
ginoauth2 "github.com/zalando/gin-oauth2"
15+
"github.com/zalando/gin-oauth2/zalando"
16+
"golang.org/x/oauth2"
17+
)
18+
19+
func TestAuthChainOptions(t *testing.T) {
20+
tokenserver := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
21+
io.Copy(os.Stderr, r.Body)
22+
t.Logf("tokenserver")
23+
println("tokenserver")
24+
w.WriteHeader(200)
25+
w.Write([]byte("token-server"))
26+
}))
27+
defer tokenserver.Close()
28+
authserver := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
29+
io.Copy(os.Stderr, r.Body)
30+
t.Logf("authserver")
31+
println("authserver")
32+
w.WriteHeader(200)
33+
w.Write([]byte("auth-server"))
34+
}))
35+
defer authserver.Close()
36+
37+
scopeStrings := []string{"uid"}
38+
checkName := "foo"
39+
endpoint := oauth2.Endpoint{
40+
AuthURL: authserver.URL,
41+
TokenURL: tokenserver.URL,
42+
}
43+
44+
ginoauth2.Auth(zalando.ScopeAndCheck(checkName, scopeStrings...), endpoint)
45+
ginoauth2.AuthChain(endpoint, zalando.ScopeAndCheck(checkName, scopeStrings...))
46+
ginoauth2.AuthChainOptions(ginoauth2.Options{Endpoint: endpoint}, zalando.ScopeAndCheck(checkName, scopeStrings...))
47+
48+
logger := logrus.New()
49+
logger.SetLevel(logrus.DebugLevel)
50+
authConfig := ginoauth2.AuthChainOptions(ginoauth2.Options{
51+
Endpoint: endpoint,
52+
AccessTokenInHeader: true,
53+
Log: logger,
54+
}, zalando.ScopeAndCheck(checkName, scopeStrings...))
55+
56+
router := gin.New()
57+
router.Use(authConfig)
58+
router.GET("/", func(c *gin.Context) {
59+
if v, ok := c.Get("cn"); ok {
60+
c.JSON(200, gin.H{"message": fmt.Sprintf("Hello from private for users to %s", v)})
61+
} else {
62+
c.JSON(200, gin.H{"message": "Hello from private for users without cn"})
63+
}
64+
})
65+
66+
w := PerformRequest(router, "GET", "/", http.Header{})
67+
if w.Code != http.StatusUnauthorized {
68+
t.Fatalf("Failed to get status 401, got: %d", w.Code)
69+
}
70+
71+
w = PerformRequest(router, "GET", "/", http.Header{"Authorization": []string{"foo"}})
72+
if w.Code != http.StatusUnauthorized {
73+
t.Fatalf("Failed to get status 401, got: %d", w.Code)
74+
}
75+
76+
token := "eyJraWQiOiJwbGF0Zm9ybS1pYW0tc2FuZGJveC0yIiwiYWxnIjoiRVMyNTYifQ.eyJzdWIiOiIxZmFjYzkyMC0xZmIzLTQwYjAtOTg4Yi0yNDhhMTE5Zjg1MmEiLCJodHRwczovL2lkZW50aXR5LnphbGFuZG8uY29tL3JlYWxtIjoidXNlcnMiLCJodHRwczovL2lkZW50aXR5LnphbGFuZG8uY29tL3Rva2VuIjoiQmVhcmVyIiwiaHR0cHM6Ly9pZGVudGl0eS56YWxhbmRvLmNvbS9tYW5hZ2VkLWlkIjoic3N6dWVjcyIsImF6cCI6Inp0b2tlbiIsImh0dHBzOi8vaWRlbnRpdHkuemFsYW5kby5jb20vYnAiOiI4MTBkMWQwMC00MzEyLTQzZTUtYmQzMS1kODM3M2ZkZDI0YzciLCJhdXRoX3RpbWUiOjE3NjE4NTQ3NzMsImlzcyI6Imh0dHBzOi8vc2FuZGJveC5pZGVudGl0eS56YWxhbmRvLmNvbSIsImV4cCI6MTc2MTg2OTE3MywiaWF0IjoxNzYxODU0NzYzfQ.aw4iaFQyHVM4EfiaoSJY9ugrCQwLAiqK_oobQn-8x6lnS2PLGY75jURz5P6Kk6sQaM6zf70GpEGIuFrEhl9HOw"
77+
w = PerformRequest(router, "GET", "/", http.Header{"Authorization": []string{"Bearer", token}})
78+
if w.Code != http.StatusOK {
79+
t.Fatalf("Failed to get status 200, got: %d", w.Code)
80+
}
81+
82+
}
83+
84+
func PerformRequest(r http.Handler, method, path string, header http.Header) *httptest.ResponseRecorder {
85+
req := httptest.NewRequest(method, path, nil)
86+
maps.Copy(req.Header, header)
87+
w := httptest.NewRecorder()
88+
r.ServeHTTP(w, req)
89+
return w
90+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/gin-gonic/gin v1.11.0
1010
github.com/golang/glog v1.2.5
1111
github.com/google/go-github v17.0.0+incompatible
12+
github.com/sirupsen/logrus v1.9.3
1213
github.com/stretchr/testify v1.11.1
1314
github.com/szuecs/gin-glog v1.1.1
1415
golang.org/x/oauth2 v0.32.0

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,13 @@ github.com/quic-go/quic-go v0.54.0 h1:6s1YB9QotYI6Ospeiguknbp2Znb/jZYjZLRXn9kMQB
9595
github.com/quic-go/quic-go v0.54.0/go.mod h1:e68ZEaCdyviluZmy44P6Iey98v/Wfz6HCjQEm+l8zTY=
9696
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
9797
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
98+
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
99+
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
98100
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
99101
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
100102
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
101103
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
104+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
102105
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
103106
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
104107
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
@@ -138,6 +141,7 @@ golang.org/x/oauth2 v0.32.0 h1:jsCblLleRMDrxMN29H3z/k1KliIvpLgCkE6R8FXXNgY=
138141
golang.org/x/oauth2 v0.32.0/go.mod h1:lzm5WQJQwKZ3nwavOZ3IS5Aulzxi68dUSgRHujetwEA=
139142
golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug=
140143
golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
144+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
141145
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
142146
golang.org/x/sys v0.36.0 h1:KVRy2GtZBrk1cBYA7MKu5bEZFxQk4NIDV6RLVcC8o0k=
143147
golang.org/x/sys v0.36.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=

0 commit comments

Comments
 (0)