@@ -5,6 +5,7 @@ const chaiHttp = require("chai-http");
5
5
const passport = require ( "passport" ) ;
6
6
const app = require ( "../../server" ) ;
7
7
const cleanDb = require ( "../utils/cleanDb" ) ;
8
+ const { generateGithubAuthRedirectUrl } = require ( "..//utils/github" ) ;
8
9
const { addUserToDBForTest } = require ( "../../utils/users" ) ;
9
10
const userData = require ( "../fixtures/user/user" ) ( ) ;
10
11
@@ -20,6 +21,25 @@ describe("auth", function () {
20
21
sinon . restore ( ) ;
21
22
} ) ;
22
23
24
+ it ( "should return github call back URL" , async function ( ) {
25
+ const githubOauthURL = generateGithubAuthRedirectUrl ( { } ) ;
26
+ const res = await chai . request ( app ) . get ( "/auth/github/login" ) . redirects ( 0 ) ;
27
+ expect ( res ) . to . have . status ( 302 ) ;
28
+ expect ( res . headers . location ) . to . equal ( githubOauthURL ) ;
29
+ } ) ;
30
+
31
+ it ( "should return github call back URL with redirectUrl" , async function ( ) {
32
+ const RDS_MEMBERS_SITE_URL = "https://members.realdevsquad.com" ;
33
+ const githubOauthURL = generateGithubAuthRedirectUrl ( { state : RDS_MEMBERS_SITE_URL } ) ;
34
+ const res = await chai
35
+ . request ( app )
36
+ . get ( "/auth/github/login" )
37
+ . query ( { redirectURL : RDS_MEMBERS_SITE_URL } )
38
+ . redirects ( 0 ) ;
39
+ expect ( res ) . to . have . status ( 302 ) ;
40
+ expect ( res . headers . location ) . to . equal ( githubOauthURL ) ;
41
+ } ) ;
42
+
23
43
it ( "should redirect the user to new sign up flow if they are have incomplte user details true" , async function ( ) {
24
44
const redirectURL = "https://my.realdevsquad.com/new-signup" ;
25
45
sinon . stub ( passport , "authenticate" ) . callsFake ( ( strategy , options , callback ) => {
@@ -38,8 +58,7 @@ describe("auth", function () {
38
58
// same data should be return from github and same data should be added there
39
59
it ( "should redirect the request to the goto page on successful login, if user has incomplete user details false" , async function ( ) {
40
60
await addUserToDBForTest ( userData [ 0 ] ) ;
41
- const rdsUiUrl = config . get ( "services.rdsUi.baseUrl" ) ;
42
-
61
+ const rdsUiUrl = new URL ( config . get ( "services.rdsUi.baseUrl" ) ) . href ;
43
62
sinon . stub ( passport , "authenticate" ) . callsFake ( ( strategy , options , callback ) => {
44
63
callback ( null , "accessToken" , githubUserInfo [ 0 ] ) ;
45
64
return ( req , res , next ) => { } ;
@@ -54,6 +73,59 @@ describe("auth", function () {
54
73
expect ( res . headers . location ) . to . equal ( rdsUiUrl ) ;
55
74
} ) ;
56
75
76
+ it ( "should redirect the request to the redirect URL provided on successful login, if user has incomplete user details false" , async function ( ) {
77
+ await addUserToDBForTest ( userData [ 0 ] ) ;
78
+ const rdsUrl = new URL ( "https://dashboard.realdevsquad.com" ) . href ;
79
+ sinon . stub ( passport , "authenticate" ) . callsFake ( ( strategy , options , callback ) => {
80
+ callback ( null , "accessToken" , githubUserInfo [ 0 ] ) ;
81
+ return ( req , res , next ) => { } ;
82
+ } ) ;
83
+
84
+ const res = await chai
85
+ . request ( app )
86
+ . get ( `/auth/github/callback` )
87
+ . query ( { code : "codeReturnedByGithub" , state : rdsUrl } )
88
+ . redirects ( 0 ) ;
89
+ expect ( res ) . to . have . status ( 302 ) ;
90
+ expect ( res . headers . location ) . to . equal ( rdsUrl ) ;
91
+ } ) ;
92
+
93
+ it ( "should redirect the realdevsquad.com if non RDS URL provided, any url that is other than *.realdevsqud.com is invalid" , async function ( ) {
94
+ await addUserToDBForTest ( userData [ 0 ] ) ;
95
+ const invalidRedirectUrl = new URL ( "https://google.com" ) . href ;
96
+ const rdsUiUrl = new URL ( config . get ( "services.rdsUi.baseUrl" ) ) . href ;
97
+ sinon . stub ( passport , "authenticate" ) . callsFake ( ( strategy , options , callback ) => {
98
+ callback ( null , "accessToken" , githubUserInfo [ 0 ] ) ;
99
+ return ( req , res , next ) => { } ;
100
+ } ) ;
101
+
102
+ const res = await chai
103
+ . request ( app )
104
+ . get ( `/auth/github/callback` )
105
+ . query ( { code : "codeReturnedByGithub" , state : invalidRedirectUrl } )
106
+ . redirects ( 0 ) ;
107
+ expect ( res ) . to . have . status ( 302 ) ;
108
+ expect ( res . headers . location ) . to . equal ( rdsUiUrl ) ;
109
+ } ) ;
110
+
111
+ it ( "should redirect the realdevsquad.com if invalid redirect URL provided" , async function ( ) {
112
+ await addUserToDBForTest ( userData [ 0 ] ) ;
113
+ const invalidRedirectUrl = "invalidURL" ;
114
+ const rdsUiUrl = new URL ( config . get ( "services.rdsUi.baseUrl" ) ) . href ;
115
+ sinon . stub ( passport , "authenticate" ) . callsFake ( ( strategy , options , callback ) => {
116
+ callback ( null , "accessToken" , githubUserInfo [ 0 ] ) ;
117
+ return ( req , res , next ) => { } ;
118
+ } ) ;
119
+
120
+ const res = await chai
121
+ . request ( app )
122
+ . get ( `/auth/github/callback` )
123
+ . query ( { code : "codeReturnedByGithub" , state : invalidRedirectUrl } )
124
+ . redirects ( 0 ) ;
125
+ expect ( res ) . to . have . status ( 302 ) ;
126
+ expect ( res . headers . location ) . to . equal ( rdsUiUrl ) ;
127
+ } ) ;
128
+
57
129
it ( "should send a cookie with JWT in the response" , function ( done ) {
58
130
const rdsUiUrl = new URL ( config . get ( "services.rdsUi.baseUrl" ) ) ;
59
131
0 commit comments