@@ -11,33 +11,101 @@ import (
1111 . "github.com/smartystreets/goconvey/convey"
1212)
1313
14+ /*
15+ Portal Proxy mock that errors out when looking for a session
16+ */
1417type sessionTestPortalProxy struct {
1518 portalProxy
1619}
1720
18- type testEchoContext struct {
21+ func (* sessionTestPortalProxy ) GetSession (_ echo.Context ) (* sessions.Session , error ) {
22+ return nil , errors .New ("Test error" )
23+ }
24+
25+ /*
26+ Echo context mock that will make everything work and bypass all session checks
27+ */
28+ type testEchoContextBypass struct {
1929 echo.Context
30+ JSONResponseVerifier func (statusCode int , input any ) error
2031}
2132
22- func (s * testEchoContext ) Request () * http.Request {
23- return nil
33+ func (* testEchoContextBypass ) Request () * http.Request {
34+ return & http. Request {}
2435}
2536
26- func (s * testEchoContext ) Get (key string ) interface {} {
37+ func (* testEchoContextBypass ) Get (_ string ) any {
2738 return nil
2839}
2940
30- func (p * sessionTestPortalProxy ) GetSession (c echo.Context ) (* sessions.Session , error ) {
31- return nil , errors .New ("Test error" )
41+ func (* testEchoContextBypass ) Set (_ string , _ any ) {}
42+
43+ func (c * testEchoContextBypass ) JSON (statusCode int , input any ) error {
44+ return c .JSONResponseVerifier (statusCode , input )
3245}
3346
34- type testSessionStore struct {
47+ type testSessionStoreFailure struct {
3548 api.SessionStorer
3649}
3750
38- func (p * testSessionStore ) Get (req * http.Request , name string ) (* sessions.Session , error ) {
51+ func (p * testSessionStoreFailure ) Get (_ * http.Request , _ string ) (* sessions.Session , error ) {
3952 return nil , errors .New ("Test error" )
4053}
54+
55+ type testSessionStoreSuccessful struct {
56+ api.SessionStorer
57+ }
58+
59+ func (p * testSessionStoreSuccessful ) Get (_ * http.Request , _ string ) (* sessions.Session , error ) {
60+ return & sessions.Session {
61+ ID : "00000000-0000-0000-0000-000000000001" ,
62+ Values : map [any ]any {
63+ "exp" : int64 (9223372036854775807 ),
64+ "user_id" : "mockuid" ,
65+ },
66+ Options : & sessions.Options {},
67+ IsNew : false ,
68+ }, nil
69+ }
70+
71+ type testStratosAuthService struct {
72+ api.StratosAuth
73+ }
74+
75+ func (s * testStratosAuthService ) BeforeVerifySession (c echo.Context ) {}
76+ func (s * testStratosAuthService ) VerifySession (c echo.Context , sessionUser string , expiryTime int64 ) error {
77+ return nil
78+ }
79+
80+ type testStoreFactory struct {
81+ api.StoreFactory
82+ }
83+
84+ func (s * testStoreFactory ) TokenStore () (api.TokenRepository , error ) {
85+ return & testTokenStore {}, nil
86+ }
87+
88+ type testTokenStore struct {
89+ api.TokenRepository
90+ }
91+
92+ func (s * testTokenStore ) FindAuthToken (userGUID string , encryptionKey []byte ) (api.TokenRecord , error ) {
93+ return api.TokenRecord {
94+ TokenGUID : "mockTokenGUID" ,
95+ AuthToken : "mockAuthToken" ,
96+ RefreshToken : "mockRefreshToken" ,
97+ TokenExpiry : 1 ,
98+ Disconnected : false ,
99+ AuthType : "mockAuthType" ,
100+ Metadata : "mockMetadata" ,
101+ SystemShared : false ,
102+ LinkedGUID : "mockLinkedGUID" ,
103+ Certificate : "mockCertificate" ,
104+ CertificateKey : "mockCertificateKey" ,
105+ Enabled : false ,
106+ }, nil
107+ }
108+
41109func TestSession (t * testing.T ) {
42110
43111 Convey ("Check error struct" , t , func () {
@@ -52,10 +120,10 @@ func TestSession(t *testing.T) {
52120 Convey ("Check error conditions when no session avaialable" , t , func () {
53121
54122 pp := & portalProxy {
55- SessionStore : & testSessionStore {},
123+ SessionStore : & testSessionStoreFailure {},
56124 }
57125
58- ctx := & testEchoContext {}
126+ ctx := & testEchoContextBypass {}
59127
60128 _ , err := pp .GetSession (ctx )
61129 So (err , ShouldNotBeNil )
@@ -74,4 +142,51 @@ func TestSession(t *testing.T) {
74142 So (err , ShouldNotBeNil )
75143 })
76144
145+ Convey ("Return the token on request for the current session when session is valid" , t , func () {
146+ pp := & portalProxy {
147+ SessionStore : & testSessionStoreSuccessful {},
148+ StratosAuthService : & testStratosAuthService {},
149+ StoreFactory : & testStoreFactory {},
150+ }
151+
152+ ctx := & testEchoContextBypass {JSONResponseVerifier : func (s int , i any ) error {
153+ So (s , ShouldEqual , http .StatusOK )
154+ t , ok := i .(AuthTokenEnvelope )
155+ So (ok , ShouldBeTrue )
156+ So (t .Error , ShouldBeEmpty )
157+ So (t .Status , ShouldEqual , "ok" )
158+ So (t .Data .AuthToken , ShouldEqual , "mockAuthToken" )
159+ So (t .Data .RefreshToken , ShouldEqual , "mockRefreshToken" )
160+ So (t .Data .TokenExpiry , ShouldEqual , 1 )
161+
162+ return nil
163+ }}
164+
165+ err := pp .retrieveToken (ctx )
166+
167+ So (err , ShouldBeNil )
168+ })
169+
170+ Convey ("Return error message and no token on request if current session is invalid" , t , func () {
171+ pp := & portalProxy {
172+ SessionStore : & testSessionStoreFailure {},
173+ StratosAuthService : & testStratosAuthService {},
174+ StoreFactory : & testStoreFactory {},
175+ }
176+
177+ ctx := & testEchoContextBypass {JSONResponseVerifier : func (s int , i any ) error {
178+ So (s , ShouldEqual , http .StatusOK )
179+ t , ok := i .(AuthTokenEnvelope )
180+ So (ok , ShouldBeTrue )
181+ So (t .Error , ShouldNotBeBlank )
182+ So (t .Status , ShouldEqual , "error" )
183+ So (t .Data , ShouldBeNil )
184+ return nil
185+ }}
186+
187+ err := pp .retrieveToken (ctx )
188+
189+ So (err , ShouldBeNil )
190+ })
191+
77192}
0 commit comments