@@ -2,7 +2,7 @@ const chai = require('chai');
22const chaiHttp = require ( 'chai-http' ) ;
33const sinon = require ( 'sinon' ) ;
44const express = require ( 'express' ) ;
5- const authRouter = require ( '../../../src/service/routes/auth' ) ;
5+ const { router , loginSuccessHandler } = require ( '../../../src/service/routes/auth' ) ;
66const db = require ( '../../../src/db' ) ;
77
88const { expect } = chai ;
@@ -19,11 +19,15 @@ const newApp = (username) => {
1919 } ) ;
2020 }
2121
22- app . use ( '/auth' , authRouter ) ;
22+ app . use ( '/auth' , router ) ;
2323 return app ;
2424} ;
2525
26- describe ( 'Authentication Routes' , ( ) => {
26+ describe ( 'Auth API' , function ( ) {
27+ afterEach ( function ( ) {
28+ sinon . restore ( ) ;
29+ } ) ;
30+
2731 describe ( '/gitAccount' , ( ) => {
2832 beforeEach ( ( ) => {
2933 sinon . stub ( db , 'findUser' ) . callsFake ( ( username ) => {
@@ -112,7 +116,7 @@ describe('Authentication Routes', () => {
112116 } ) ,
113117 ) . to . be . true ;
114118 } ) ;
115-
119+
116120 it ( 'POST /gitAccount allows non-admin user to update their own gitAccount' , async ( ) => {
117121 const updateUserStub = sinon . stub ( db , 'updateUser' ) . resolves ( ) ;
118122
@@ -132,6 +136,93 @@ describe('Authentication Routes', () => {
132136 } ) ,
133137 ) . to . be . true ;
134138 } ) ;
139+ } ) ;
140+
141+ describe ( 'loginSuccessHandler' , function ( ) {
142+ it ( 'should log in user and return public user data' , async function ( ) {
143+ const user = {
144+ username : 'bob' ,
145+ password : 'secret' ,
146+ email : 'bob@example.com' ,
147+ displayName : 'Bob' ,
148+ } ;
135149
150+ const res = {
151+ send : sinon . spy ( ) ,
152+ } ;
153+
154+ await loginSuccessHandler ( ) ( { user } , res ) ;
155+
156+ expect ( res . send . calledOnce ) . to . be . true ;
157+ expect ( res . send . firstCall . args [ 0 ] ) . to . deep . equal ( {
158+ message : 'success' ,
159+ user : {
160+ admin : false ,
161+ displayName : 'Bob' ,
162+ email : 'bob@example.com' ,
163+ gitAccount : '' ,
164+ title : '' ,
165+ username : 'bob' ,
166+ } ,
167+ } ) ;
168+ } ) ;
169+ } ) ;
170+
171+ describe ( '/me' , function ( ) {
172+ it ( 'GET /me returns Unauthorized if authenticated user not in request' , async ( ) => {
173+ const res = await chai . request ( newApp ( ) ) . get ( '/auth/me' ) ;
174+
175+ expect ( res ) . to . have . status ( 401 ) ;
176+ } ) ;
177+
178+ it ( 'GET /me serializes public data representation of current authenticated user' , async function ( ) {
179+ sinon . stub ( db , 'findUser' ) . resolves ( {
180+ username : 'alice' ,
181+ password : 'secret-hashed-password' ,
182+ email : 'alice@example.com' ,
183+ displayName : 'Alice Walker' ,
184+ otherUserData : 'should not be returned' ,
185+ } ) ;
186+
187+ const res = await chai . request ( newApp ( 'alice' ) ) . get ( '/auth/me' ) ;
188+ expect ( res ) . to . have . status ( 200 ) ;
189+ expect ( res . body ) . to . deep . equal ( {
190+ username : 'alice' ,
191+ displayName : 'Alice Walker' ,
192+ email : 'alice@example.com' ,
193+ title : '' ,
194+ gitAccount : '' ,
195+ admin : false ,
196+ } ) ;
197+ } ) ;
198+ } ) ;
199+
200+ describe ( '/profile' , function ( ) {
201+ it ( 'GET /profile returns Unauthorized if authenticated user not in request' , async ( ) => {
202+ const res = await chai . request ( newApp ( ) ) . get ( '/auth/profile' ) ;
203+
204+ expect ( res ) . to . have . status ( 401 ) ;
205+ } ) ;
206+
207+ it ( 'GET /profile serializes public data representation of current authenticated user' , async function ( ) {
208+ sinon . stub ( db , 'findUser' ) . resolves ( {
209+ username : 'alice' ,
210+ password : 'secret-hashed-password' ,
211+ email : 'alice@example.com' ,
212+ displayName : 'Alice Walker' ,
213+ otherUserData : 'should not be returned' ,
214+ } ) ;
215+
216+ const res = await chai . request ( newApp ( 'alice' ) ) . get ( '/auth/profile' ) ;
217+ expect ( res ) . to . have . status ( 200 ) ;
218+ expect ( res . body ) . to . deep . equal ( {
219+ username : 'alice' ,
220+ displayName : 'Alice Walker' ,
221+ email : 'alice@example.com' ,
222+ title : '' ,
223+ gitAccount : '' ,
224+ admin : false ,
225+ } ) ;
226+ } ) ;
136227 } ) ;
137228} ) ;
0 commit comments