@@ -63,6 +63,17 @@ describe('Testing SSOTokenData Class', () => {
6363 // expect(err).toEqual('Secret must be a string value');
6464 // });
6565 // });
66+ test ( 'Test secret to be non string with callback' , ( done ) => {
67+ SSOTokenDataObj . getSigned ( { } , ( err ) => {
68+ expect ( err ) . toBe ( 'Secret must be a string value' ) ;
69+ done ( ) ;
70+ } ) ;
71+ } ) ;
72+ test ( 'Should throw if callback is not a function' , ( ) => {
73+ expect ( ( ) => {
74+ SSOTokenDataObj . getSigned ( secretPub , 'notAFunction' ) ;
75+ } ) . toThrow ( 'Callback must be a function' ) ;
76+ } ) ;
6677 test ( 'Should return error if no secret specified' , ( done ) => {
6778 SSOTokenDataObj . getSigned ( null , ( err , signed ) => {
6879 expect ( err ) . toBe ( 'No secret specified' ) ;
@@ -78,4 +89,144 @@ describe('Testing SSOTokenData Class', () => {
7889 } ) ;
7990 } ) ;
8091 } ) ;
92+
93+ describe ( 'Testing SSOTokenData._getSignedWrong' , ( ) => {
94+ describe ( 'Sync mode' , ( ) => {
95+ test ( 'Should throw if no secret specified' , ( ) => {
96+ expect ( ( ) => {
97+ SSOTokenDataObj . _getSignedWrong ( ) ;
98+ } ) . toThrow ( 'No secret specified' ) ;
99+ } ) ;
100+ test ( 'Should return signed value with any string secret (HS256 default)' , ( ) => {
101+ const signed = SSOTokenDataObj . _getSignedWrong ( 'simple-secret' ) ;
102+ expect ( signed ) . toBeDefined ( ) ;
103+ } ) ;
104+ } ) ;
105+ describe ( 'Async mode' , ( ) => {
106+ test ( 'Should call callback with "No secret specified" when null secret and cb provided' , ( ) => {
107+ const calls = [ ] ;
108+ SSOTokenDataObj . _getSignedWrong ( null , ( err ) => {
109+ calls . push ( err ) ;
110+ } ) ;
111+ expect ( calls [ 0 ] ) . toBe ( 'No secret specified' ) ;
112+ } ) ;
113+ test ( 'Should call callback with signed value if secret specified' , ( done ) => {
114+ SSOTokenDataObj . _getSignedWrong ( 'simple-secret' , ( err , signed ) => {
115+ expect ( err ) . toBeFalsy ( ) ;
116+ expect ( signed ) . toBeDefined ( ) ;
117+ done ( ) ;
118+ } ) ;
119+ } ) ;
120+ } ) ;
121+ describe ( 'Catch block' , ( ) => {
122+ test ( 'Should throw when jwt.sign throws internally' , ( ) => {
123+ const jwt = require ( 'jsonwebtoken' ) ;
124+ jest . spyOn ( jwt , 'sign' ) . mockImplementationOnce ( ( ) => {
125+ throw new Error ( 'mocked jwt error' ) ;
126+ } ) ;
127+ expect ( ( ) => {
128+ SSOTokenDataObj . _getSignedWrong ( 'some-secret' ) ;
129+ } ) . toThrow ( 'mocked jwt error' ) ;
130+ jest . restoreAllMocks ( ) ;
131+ } ) ;
132+ } ) ;
133+ } ) ;
134+
135+ describe ( 'Testing getSigned catch block' , ( ) => {
136+ test ( 'getSigned returns undefined when jwt.sign throws with invalid RSA key' , ( ) => {
137+ const result = SSOTokenDataObj . getSigned ( 'not-a-valid-rsa-key' ) ;
138+ expect ( result ) . toBeUndefined ( ) ;
139+ } ) ;
140+ } ) ;
141+ } ) ;
142+
143+ describe ( 'Testing SSOTokenData getter methods' , ( ) => {
144+ test ( 'getBranchId returns correct value' , ( ) => {
145+ expect ( SSOTokenDataObj . getBranchId ( ) ) . toBe ( '5e3bfa789f436c5e2ee5141a' ) ;
146+ } ) ;
147+ test ( 'getBranchSlug returns correct value' , ( ) => {
148+ expect ( SSOTokenDataObj . getBranchSlug ( ) ) . toBe ( 'staffbase' ) ;
149+ } ) ;
150+ test ( 'getAudience returns correct value' , ( ) => {
151+ expect ( SSOTokenDataObj . getAudience ( ) ) . toBe ( 'testPlugin' ) ;
152+ } ) ;
153+ test ( 'getExpireAtTime returns correct value' , ( ) => {
154+ expect ( SSOTokenDataObj . getExpireAtTime ( ) ) . toBe ( tokenDataVals . CLAIM_EXPIRE_AT ) ;
155+ } ) ;
156+ test ( 'getNotBeforeTime returns correct value' , ( ) => {
157+ expect ( SSOTokenDataObj . getNotBeforeTime ( ) ) . toBe ( tokenDataVals . CLAIM_NOT_BEFORE ) ;
158+ } ) ;
159+ test ( 'getIssuedAtTime returns correct value' , ( ) => {
160+ expect ( SSOTokenDataObj . getIssuedAtTime ( ) ) . toBe ( tokenDataVals . CLAIM_ISSUED_AT ) ;
161+ } ) ;
162+ test ( 'getIssuer returns correct value' , ( ) => {
163+ expect ( SSOTokenDataObj . getIssuer ( ) ) . toBe ( 'api.staffbase.com' ) ;
164+ } ) ;
165+ test ( 'getInstanceId returns correct value' , ( ) => {
166+ expect ( SSOTokenDataObj . getInstanceId ( ) ) . toBe ( '55c79b6ee4b06c6fb19bd1e2' ) ;
167+ } ) ;
168+ test ( 'getInstanceName returns correct value' , ( ) => {
169+ expect ( SSOTokenDataObj . getInstanceName ( ) ) . toBe ( 'Our locations' ) ;
170+ } ) ;
171+ test ( 'getUserId returns correct value' , ( ) => {
172+ expect ( SSOTokenDataObj . getUserId ( ) ) . toBe ( '541954c3e4b08bbdce1a340a' ) ;
173+ } ) ;
174+ test ( 'getUserExternalId returns correct value' , ( ) => {
175+ expect ( SSOTokenDataObj . getUserExternalId ( ) ) . toBe ( 'jdoe' ) ;
176+ } ) ;
177+ test ( 'getUserUsername returns correct value' , ( ) => {
178+ expect ( SSOTokenDataObj . getUserUsername ( ) ) . toBe ( 'john.doe' ) ;
179+ } ) ;
180+ test ( 'getUserPrimaryEmailAddress returns correct value' , ( ) => {
181+ expect ( SSOTokenDataObj . getUserPrimaryEmailAddress ( ) ) . toBe ( 'jdoe@email.com' ) ;
182+ } ) ;
183+ test ( 'getFullName returns correct value' , ( ) => {
184+ expect ( SSOTokenDataObj . getFullName ( ) ) . toBe ( 'John Doe' ) ;
185+ } ) ;
186+ test ( 'getFirstName returns correct value' , ( ) => {
187+ expect ( SSOTokenDataObj . getFirstName ( ) ) . toBe ( 'John' ) ;
188+ } ) ;
189+ test ( 'getLastName returns correct value' , ( ) => {
190+ expect ( SSOTokenDataObj . getLastName ( ) ) . toBe ( 'Doe' ) ;
191+ } ) ;
192+ test ( 'getRole returns correct value' , ( ) => {
193+ expect ( SSOTokenDataObj . getRole ( ) ) . toBe ( 'editor' ) ;
194+ } ) ;
195+ test ( 'getType returns correct value' , ( ) => {
196+ expect ( SSOTokenDataObj . getType ( ) ) . toBe ( 'type' ) ;
197+ } ) ;
198+ test ( 'getThemeTextColor returns correct value' , ( ) => {
199+ expect ( SSOTokenDataObj . getThemeTextColor ( ) ) . toBe ( '#00ABAB' ) ;
200+ } ) ;
201+ test ( 'getThemeBackgroundColor returns correct value' , ( ) => {
202+ expect ( SSOTokenDataObj . getThemeBackgroundColor ( ) ) . toBe ( '#FFAABB' ) ;
203+ } ) ;
204+ test ( 'getLocale returns correct value' , ( ) => {
205+ expect ( SSOTokenDataObj . getLocale ( ) ) . toBe ( 'en-US' ) ;
206+ } ) ;
207+ test ( 'isEditor returns true for editor role' , ( ) => {
208+ expect ( SSOTokenDataObj . isEditor ( ) ) . toBe ( true ) ;
209+ } ) ;
210+ test ( 'isEditor returns false for non-editor role' , ( ) => {
211+ const userTokenData = new SSOTokenData ( { ...tokenDataVals , CLAIM_USER_ROLE : 'user' } ) ;
212+ expect ( userTokenData . isEditor ( ) ) . toBe ( false ) ;
213+ } ) ;
214+ test ( 'getTags returns null when no tags' , ( ) => {
215+ expect ( SSOTokenDataObj . getTags ( ) ) . toBeNull ( ) ;
216+ } ) ;
217+ test ( '_getClaim throws for invalid claim name' , ( ) => {
218+ expect ( ( ) => {
219+ SSOTokenDataObj . _getClaim ( 'INVALID_CLAIM' ) ;
220+ } ) . toThrow ( 'Invalid Claim' ) ;
221+ } ) ;
222+ test ( 'toJSObj returns correct structure' , ( ) => {
223+ const obj = SSOTokenDataObj . toJSObj ( ) ;
224+ expect ( obj . aud ) . toBe ( 'testPlugin' ) ;
225+ expect ( obj . sub ) . toBe ( '541954c3e4b08bbdce1a340a' ) ;
226+ } ) ;
227+ test ( 'toJSObjPretty returns correct structure' , ( ) => {
228+ const obj = SSOTokenDataObj . toJSObjPretty ( ) ;
229+ expect ( obj . CLAIM_AUDIENCE ) . toBe ( 'testPlugin' ) ;
230+ expect ( obj . CLAIM_USER_ID ) . toBe ( '541954c3e4b08bbdce1a340a' ) ;
231+ } ) ;
81232} ) ;
0 commit comments