@@ -11,119 +11,108 @@ export default (
1111 let client : Application
1212 let user : any
1313
14- before ( ( ) =>
15- getApp ( )
16- . service ( 'users' )
17- . create ( {
14+ before (
15+ async ( ) =>
16+ ( user = await getApp ( ) . service ( 'users' ) . create ( {
1817 email,
1918 password
20- } )
21- . then ( ( result : any ) => {
22- user = result
23- } )
19+ } ) )
2420 )
2521
2622 beforeEach ( ( ) => {
2723 client = getClient ( )
2824 } )
2925
30- it ( 'authenticates with local strategy' , ( ) => {
31- return client
32- . authenticate ( {
33- strategy : 'local' ,
34- email,
35- password
36- } )
37- . then ( ( result ) => {
38- assert . ok ( result . accessToken )
39- assert . strictEqual ( result . authentication . strategy , 'local' )
40- assert . strictEqual ( result . user . email , email )
41- } )
26+ after ( async ( ) => {
27+ await getApp ( ) . service ( 'users' ) . remove ( user . id )
4228 } )
4329
44- it ( 'authentication with wrong credentials fails, does not maintain state' , ( ) => {
45- return client
46- . authenticate ( {
47- strategy : 'local' ,
48- email,
49- password : 'blabla'
50- } )
51- . then ( ( ) => assert . fail ( 'Should never get here' ) )
52- . catch ( ( error ) => {
53- assert . strictEqual ( error . name , 'NotAuthenticated' )
54- assert . strictEqual ( error . message , 'Invalid login' )
55- assert . ok ( ! client . get ( 'authentication' ) , 'Reset client state' )
56- } )
30+ it ( 'authenticates with local strategy' , async ( ) => {
31+ const result = await client . authenticate ( {
32+ strategy : 'local' ,
33+ email,
34+ password
35+ } )
36+
37+ assert . ok ( result . accessToken )
38+ assert . strictEqual ( result . authentication . strategy , 'local' )
39+ assert . strictEqual ( result . user . email , email )
5740 } )
5841
59- it ( 'errors when not authenticated' , ( ) => {
60- return client
61- . service ( 'dummy' )
62- . find ( )
63- . then ( ( ) => assert . fail ( 'Should never get here' ) )
64- . catch ( ( error : any ) => {
65- assert . strictEqual ( error . name , 'NotAuthenticated' )
66- assert . strictEqual ( error . code , 401 )
67- assert . strictEqual ( error . message , 'Not authenticated' )
68- } )
42+ it ( 'authentication with wrong credentials fails, does not maintain state' , async ( ) => {
43+ await assert . rejects (
44+ ( ) =>
45+ client . authenticate ( {
46+ strategy : 'local' ,
47+ email,
48+ password : 'blabla'
49+ } ) ,
50+ {
51+ name : 'NotAuthenticated' ,
52+ message : 'Invalid login'
53+ }
54+ )
55+ assert . ok ( ! client . get ( 'authentication' ) , 'Reset client state' )
6956 } )
7057
71- it ( 'authenticates and allows access' , ( ) => {
72- return client
73- . authenticate ( {
74- strategy : 'local' ,
75- email,
76- password
77- } )
78- . then ( ( ) => client . service ( 'dummy' ) . find ( ) )
79- . then ( ( result ) => {
80- assert . strictEqual ( result . provider , provider )
81- assert . ok ( result . authentication )
82- assert . ok ( result . authentication . payload )
83- assert . strictEqual ( result . user . email , user . email )
84- assert . strictEqual ( result . user . id , user . id )
85- } )
58+ it ( 'errors when not authenticated' , async ( ) => {
59+ await assert . rejects ( ( ) => client . service ( 'dummy' ) . find ( ) , {
60+ name : 'NotAuthenticated' ,
61+ code : 401 ,
62+ message : 'Not authenticated'
63+ } )
8664 } )
8765
88- it ( 're-authenticates' , ( ) => {
89- return client
90- . authenticate ( {
91- strategy : 'local' ,
92- email,
93- password
94- } )
95- . then ( ( ) => client . authentication . reset ( ) )
96- . then ( ( ) => client . authenticate ( ) )
97- . then ( ( ) => client . service ( 'dummy' ) . find ( ) )
98- . then ( ( result ) => {
99- assert . strictEqual ( result . provider , provider )
100- assert . ok ( result . authentication )
101- assert . ok ( result . authentication . payload )
102- assert . strictEqual ( result . user . email , user . email )
103- assert . strictEqual ( result . user . id , user . id )
104- } )
66+ it ( 'authenticates and allows access' , async ( ) => {
67+ await client . authenticate ( {
68+ strategy : 'local' ,
69+ email,
70+ password
71+ } )
72+ const result = await client . service ( 'dummy' ) . find ( )
73+
74+ assert . strictEqual ( result . provider , provider )
75+ assert . ok ( result . authentication )
76+ assert . ok ( result . authentication . payload )
77+ assert . strictEqual ( result . user . email , user . email )
78+ assert . strictEqual ( result . user . id , user . id )
10579 } )
10680
107- it ( 'after logout does not allow subsequent access' , ( ) => {
108- return client
109- . authenticate ( {
110- strategy : 'local' ,
111- email,
112- password
113- } )
114- . then ( ( ) => client . logout ( ) )
115- . then ( ( result ) => {
116- assert . ok ( result . accessToken )
117- assert . ok ( result . user )
118-
119- return client . service ( 'dummy' ) . find ( )
120- } )
121- . then ( ( ) => assert . fail ( 'Should never get here' ) )
122- . catch ( ( error ) => {
123- assert . strictEqual ( error . name , 'NotAuthenticated' )
124- assert . strictEqual ( error . code , 401 )
125- assert . strictEqual ( error . message , 'Not authenticated' )
126- } )
81+ it ( 're-authenticates' , async ( ) => {
82+ await client . authenticate ( {
83+ strategy : 'local' ,
84+ email,
85+ password
86+ } )
87+
88+ client . authentication . reset ( )
89+ client . authenticate ( )
90+ const result = await client . service ( 'dummy' ) . find ( )
91+
92+ assert . strictEqual ( result . provider , provider )
93+ assert . ok ( result . authentication )
94+ assert . ok ( result . authentication . payload )
95+ assert . strictEqual ( result . user . email , user . email )
96+ assert . strictEqual ( result . user . id , user . id )
97+ } )
98+
99+ it ( 'after logout does not allow subsequent access' , async ( ) => {
100+ await client . authenticate ( {
101+ strategy : 'local' ,
102+ email,
103+ password
104+ } )
105+
106+ const result = await client . logout ( )
107+
108+ assert . ok ( result ! . accessToken )
109+ assert . ok ( result ! . user )
110+
111+ assert . rejects ( ( ) => client . service ( 'dummy' ) . find ( ) , {
112+ name : 'NotAuthenticated' ,
113+ code : 401 ,
114+ message : 'Not authenticated'
115+ } )
127116 } )
128117 } )
129118}
0 commit comments