@@ -4,10 +4,15 @@ import (
44 "encoding/json"
55 "fmt"
66 "io/ioutil"
7+ "net/http/httptest"
78 "os"
89 "testing"
910 "time"
1011
12+ "github.com/stretchr/testify/assert"
13+
14+ "github.com/gin-gonic/gin"
15+
1116 "github.com/zalando/gin-oauth2"
1217 "golang.org/x/oauth2"
1318)
@@ -61,3 +66,66 @@ func TestRequestTeamInfo(t *testing.T) {
6166 }
6267 fmt .Printf ("%+v\n " , data )
6368}
69+
70+ func TestScopeCheck (t * testing.T ) {
71+ // given
72+ tc := & ginoauth2.TokenContainer {
73+ Token : & oauth2.Token {
74+ AccessToken : "sdgergSgadGSAHBSHsagsdv." ,
75+ TokenType : "Bearer" ,
76+ RefreshToken : "" ,
77+ },
78+ Scopes : map [string ]interface {}{
79+ "my-scope-1" : true ,
80+ "my-scope-2" : true ,
81+ "uid" : "stups_marilyn-updater" ,
82+ },
83+ GrantType : "password" ,
84+ Realm : "/services" ,
85+ }
86+ ctx , _ := gin .CreateTestContext (httptest .NewRecorder ())
87+
88+ // when
89+ checkFn := ScopeCheck ("name" , "my-scope-1" )
90+ result := checkFn (tc , ctx )
91+
92+ // then
93+ assert .True (t , result )
94+
95+ scopeVal , scopeOk := ctx .Get ("my-scope-1" )
96+ assert .True (t , scopeOk )
97+ assert .Equal (t , true , scopeVal )
98+ }
99+
100+ func TestScopeAndCheck (t * testing.T ) {
101+ // given
102+ tc := & ginoauth2.TokenContainer {
103+ Token : & oauth2.Token {
104+ AccessToken : "sdgergSgadGSAHBSHsagsdv." ,
105+ TokenType : "Bearer" ,
106+ RefreshToken : "" ,
107+ },
108+ Scopes : map [string ]interface {}{
109+ "my-scope-1" : true ,
110+ "my-scope-2" : true ,
111+ "uid" : "stups_marilyn-updater" ,
112+ },
113+ GrantType : "password" ,
114+ Realm : "/services" ,
115+ }
116+ ctx , _ := gin .CreateTestContext (httptest .NewRecorder ())
117+
118+ // when
119+ checkFn := ScopeAndCheck ("name" , "uid" , "my-scope-2" )
120+ result := checkFn (tc , ctx )
121+
122+ // then
123+ assert .True (t , result )
124+
125+ uidVal , uidOk := ctx .Get ("uid" )
126+ scopeVal , scopeOk := ctx .Get ("my-scope-2" )
127+ assert .True (t , uidOk )
128+ assert .Equal (t , "stups_marilyn-updater" , uidVal )
129+ assert .True (t , scopeOk )
130+ assert .Equal (t , true , scopeVal )
131+ }
0 commit comments