@@ -2,6 +2,7 @@ import Log from '../src/Log';
22import UserInfoService from '../src/UserInfoService' ;
33
44import StubJsonService from './StubJsonService' ;
5+ import StubMetadataService from './StubMetadataService' ;
56
67import chai from 'chai' ;
78chai . should ( ) ;
@@ -11,11 +12,13 @@ describe("UserInfoService", function() {
1112 let subject ;
1213 let settings ;
1314 let stubJsonService ;
15+ let stubMetadataService ;
1416
1517 beforeEach ( function ( ) {
1618 settings = { } ;
1719 stubJsonService = new StubJsonService ( ) ;
18- subject = new UserInfoService ( settings , ( ) => stubJsonService ) ;
20+ stubMetadataService = new StubMetadataService ( ) ;
21+ subject = new UserInfoService ( settings , ( ) => stubJsonService , ( ) => stubMetadataService ) ;
1922 } ) ;
2023
2124 describe ( "constructor" , function ( ) {
@@ -32,10 +35,90 @@ describe("UserInfoService", function() {
3235
3336 } ) ;
3437
35- describe ( "getUserInfo " , function ( ) {
38+ describe ( "getClaims " , function ( ) {
3639
3740 it ( "should return a promise" , function ( ) {
38- subject . getUserInfo ( ) . should . be . instanceof ( Promise ) ;
41+ subject . getClaims ( ) . should . be . instanceof ( Promise ) ;
42+ } ) ;
43+
44+ it ( "should require a token" , function ( done ) {
45+ subject . getClaims ( ) . then ( null ,
46+ err => {
47+ err . message . should . contain ( "token" ) ;
48+ done ( ) ;
49+ } ) ;
50+ } ) ;
51+
52+ it ( "should call userinfo url and pass token" , function ( done ) {
53+ stubMetadataService . userInfoUrlResult = Promise . resolve ( "http://sts/userinfo" ) ;
54+ stubJsonService . result = Promise . resolve ( "test" ) ;
55+
56+ subject . getClaims ( "token" ) . then ( claims => {
57+ stubJsonService . url . should . equal ( "http://sts/userinfo" ) ;
58+ stubJsonService . token . should . equal ( "token" ) ;
59+ done ( ) ;
60+ } ) ;
61+
62+ } ) ;
63+
64+ it ( "should fail when dependencies fail" , function ( done ) {
65+ stubMetadataService . userInfoUrlResult = Promise . reject ( "test" ) ;
66+
67+ subject . getClaims ( "token" ) . then ( null ,
68+ err => {
69+ err . message . should . contain ( 'claims' ) ;
70+ done ( ) ;
71+ }
72+ ) ;
73+
74+ } ) ;
75+
76+ it ( "should return claims" , function ( done ) {
77+ stubMetadataService . userInfoUrlResult = Promise . resolve ( "http://sts/userinfo" ) ;
78+ stubJsonService . result = Promise . resolve ( {
79+ foo : 1 , bar : 'test' ,
80+ aud :'some_aud' , iss :'issuer' ,
81+ sub :'123' , email :'foo@gmail.com' ,
82+ role :[ 'admin' , 'dev' ] ,
83+ nonce :'nonce' , at_hash :"athash" ,
84+ iat :5 , nbf :10 , exp :20
85+ } ) ;
86+
87+ subject . getClaims ( "token" ) . then ( claims => {
88+ claims . should . deep . equal ( {
89+ foo : 1 , bar : 'test' ,
90+ aud :'some_aud' , iss :'issuer' ,
91+ sub :'123' , email :'foo@gmail.com' ,
92+ role :[ 'admin' , 'dev' ] ,
93+ nonce :'nonce' , at_hash :"athash" ,
94+ iat :5 , nbf :10 , exp :20
95+ } ) ;
96+ done ( ) ;
97+ } ) ;
98+
99+ } ) ;
100+
101+ it ( "should filter protocol claims" , function ( done ) {
102+ stubMetadataService . userInfoUrlResult = Promise . resolve ( "http://sts/userinfo" ) ;
103+ stubJsonService . result = Promise . resolve ( {
104+ foo : 1 , bar : 'test' ,
105+ aud :'some_aud' , iss :'issuer' ,
106+ sub :'123' , email :'foo@gmail.com' ,
107+ role :[ 'admin' , 'dev' ] ,
108+ nonce :'nonce' , at_hash :"athash" ,
109+ iat :5 , nbf :10 , exp :20
110+ } ) ;
111+ settings . filterProtocolClaims = true ;
112+
113+ subject . getClaims ( "token" ) . then ( claims => {
114+ claims . should . deep . equal ( {
115+ foo : 1 , bar : 'test' ,
116+ sub :'123' , email :'foo@gmail.com' ,
117+ role :[ 'admin' , 'dev' ]
118+ } ) ;
119+ done ( ) ;
120+ } ) ;
121+
39122 } ) ;
40123
41124 } ) ;
0 commit comments