@@ -109,7 +109,15 @@ class TestPatchUserRoute extends AnyFunSuite with BeforeAndAfterAll with BeforeA
109109 modifiedAt = TIMESTAMP ,
110110 organization = TESTORGS (0 ).orgId,
111111 password = Option (Password .hash(ORG1USERPASSWORD )),
112- username = " orgUser2" ))
112+ username = " orgUser2" ),
113+ UserRow (createdAt = TIMESTAMP ,
114+ isHubAdmin = false ,
115+ isOrgAdmin = false ,
116+ modifiedAt = TIMESTAMP ,
117+ organization = TESTORGS (0 ).orgId,
118+ password = None ,
119+ identityProvider = " External OAuth" ,
120+ username = " externalUser" ))
113121 }
114122
115123 private val TESTAGBOTS : Seq [AgbotRow ] =
@@ -205,33 +213,33 @@ class TestPatchUserRoute extends AnyFunSuite with BeforeAndAfterAll with BeforeA
205213 }
206214
207215 def withOauthDisabled (testCode : => Unit ): Unit = {
208- val oauthEnabled = Configuration .getConfig.getBoolean (" api.oauth.enabled " )
216+ val oauthEnabled = Configuration .getConfig.hasPath (" api.authentication. oauth.provider.user_info.url " )
209217 assume(! oauthEnabled || runningLocally, " Skipping: OAuth mode enabled and not running locally" )
210218
211219 if (oauthEnabled && runningLocally) {
212- updateConfig(" api.oauth.enabled " , " false " )
220+ updateConfig(" api.authentication. oauth.provider.user_info.url " , " " )
213221 }
214222 try {
215223 testCode
216224 } finally {
217225 if (oauthEnabled && runningLocally) {
218- updateConfig(" api.oauth.enabled " , " true " )
226+ updateConfig(" api.authentication. oauth.provider.user_info.url " , " http://localhost:8080/mock-oauth " )
219227 }
220228 }
221229 }
222230
223231 def withOauthEnabled (testCode : => Unit ): Unit = {
224- val oauthEnabled = Configuration .getConfig.getBoolean (" api.oauth.enabled " )
232+ val oauthEnabled = Configuration .getConfig.hasPath (" api.authentication. oauth.provider.user_info.url " )
225233 assume(oauthEnabled || runningLocally, " Skipping: OAuth mode disabled and not running locally" )
226234
227235 if (! oauthEnabled && runningLocally) {
228- updateConfig(" api.oauth.enabled " , " true " )
229- }
236+ updateConfig(" api.authentication. oauth.provider.user_info.url " , " http://localhost:8080/mock-oauth " )
237+ }
230238 try {
231239 testCode
232240 } finally {
233241 if (! oauthEnabled && runningLocally) {
234- updateConfig(" api.oauth.enabled " , " false " )
242+ updateConfig(" api.authentication. oauth.provider.user_info.url " , " " )
235243 }
236244 }
237245 }
@@ -578,8 +586,9 @@ class TestPatchUserRoute extends AnyFunSuite with BeforeAndAfterAll with BeforeA
578586 assert(newUser.modifiedAt.after(TESTUSERS (2 ).modifiedAt))
579587 }
580588 }
581- // OAuth mode tests
582- test(" PATCH /orgs/" + TESTORGS (0 ).orgId + ROUTE + normalUsernameToUpdate + " -- OAuth mode blocks password modification -- 400 bad input" ) {
589+
590+ // OAuth mode tests
591+ test(" PATCH /orgs/" + TESTORGS (0 ).orgId + ROUTE + normalUsernameToUpdate + " -- OAuth mode allows password modification for local user -- 201 OK" ) {
583592 withOauthEnabled {
584593 val requestBody : PatchUsersRequest = PatchUsersRequest (
585594 password = Some (" newPassword" ),
@@ -590,14 +599,32 @@ class TestPatchUserRoute extends AnyFunSuite with BeforeAndAfterAll with BeforeA
590599 val response : HttpResponse [String ] = Http (URL + TESTORGS (0 ).orgId + ROUTE + normalUsernameToUpdate).postData(Serialization .write(requestBody)).method(" PATCH" ).headers(ACCEPT ).headers(CONTENT ).headers(ORG1ADMINAUTH ).asString
591600 info(" Code: " + response.code)
592601 info(" Body: " + response.body)
602+ assert(response.code === HttpCode .POST_OK .intValue)
603+
604+ val updatedUser : UserRow = Await .result(DBCONNECTION .run(UsersTQ .filter(_.user === TESTUSERS (2 ).user).result), AWAITDURATION ).head
605+ assert(updatedUser.password !== TESTUSERS (2 ).password) // Password should be changed
606+ }
607+ }
608+
609+ test(" PATCH /orgs/" + TESTORGS (0 ).orgId + ROUTE + TESTUSERS (5 ).username + " -- OAuth mode blocks password modification for external user -- 400 bad input" ) {
610+ withOauthEnabled {
611+ val requestBody : PatchUsersRequest = PatchUsersRequest (
612+ password = Some (" newPassword" ),
613+ admin = None ,
614+ hubAdmin = None ,
615+ email = None
616+ )
617+ val response : HttpResponse [String ] = Http (URL + TESTORGS (0 ).orgId + ROUTE + TESTUSERS (5 ).username).postData(Serialization .write(requestBody)).method(" PATCH" ).headers(ACCEPT ).headers(CONTENT ).headers(ORG1ADMINAUTH ).asString
618+ info(" Code: " + response.code)
619+ info(" Body: " + response.body)
593620 assert(response.code === HttpCode .BAD_INPUT .intValue)
594621 val responseBody : ApiResponse = JsonMethods .parse(response.body).extract[ApiResponse ]
595622 assert(responseBody.msg === ExchMsg .translate(" user.attr.not.allowed.oauth" , " password" ))
596- assertNoChanges(TESTUSERS (2 ))
623+ assertNoChanges(TESTUSERS (5 ))
597624 }
598625 }
599626
600- test(" PATCH /orgs/" + TESTORGS (0 ).orgId + ROUTE + normalUsernameToUpdate + " -- OAuth mode blocks email modification -- 400 bad input " ) {
627+ test(" PATCH /orgs/" + TESTORGS (0 ).orgId + ROUTE + normalUsernameToUpdate + " -- OAuth mode allows email modification for local user -- 201 OK " ) {
601628 withOauthEnabled {
602629 val requestBody : PatchUsersRequest = PatchUsersRequest (
603630 password = None ,
@@ -608,14 +635,32 @@ class TestPatchUserRoute extends AnyFunSuite with BeforeAndAfterAll with BeforeA
608635 val response : HttpResponse [String ] = Http (URL + TESTORGS (0 ).orgId + ROUTE + normalUsernameToUpdate).postData(Serialization .write(requestBody)).method(" PATCH" ).headers(ACCEPT ).headers(CONTENT ).headers(ORG1ADMINAUTH ).asString
609636 info(" Code: " + response.code)
610637 info(" Body: " + response.body)
638+ assert(response.code === HttpCode .POST_OK .intValue)
639+
640+ val updatedUser : UserRow = Await .result(DBCONNECTION .run(UsersTQ .filter(_.user === TESTUSERS (2 ).user).result), AWAITDURATION ).head
641+ assert(updatedUser.email === Some (" newEmail@example.com" ))
642+ }
643+ }
644+
645+ test(" PATCH /orgs/" + TESTORGS (0 ).orgId + ROUTE + TESTUSERS (5 ).username + " -- OAuth mode blocks email modification for external user -- 400 bad input" ) {
646+ withOauthEnabled {
647+ val requestBody : PatchUsersRequest = PatchUsersRequest (
648+ password = None ,
649+ admin = None ,
650+ hubAdmin = None ,
651+ email = Some (" newEmail@example.com" )
652+ )
653+ val response : HttpResponse [String ] = Http (URL + TESTORGS (0 ).orgId + ROUTE + TESTUSERS (5 ).username).postData(Serialization .write(requestBody)).method(" PATCH" ).headers(ACCEPT ).headers(CONTENT ).headers(ORG1ADMINAUTH ).asString
654+ info(" Code: " + response.code)
655+ info(" Body: " + response.body)
611656 assert(response.code === HttpCode .BAD_INPUT .intValue)
612657 val responseBody : ApiResponse = JsonMethods .parse(response.body).extract[ApiResponse ]
613658 assert(responseBody.msg === ExchMsg .translate(" user.attr.not.allowed.oauth" , " email" ))
614- assertNoChanges(TESTUSERS (2 ))
659+ assertNoChanges(TESTUSERS (5 ))
615660 }
616661 }
617662
618- test(" PATCH /orgs/" + TESTORGS (0 ).orgId + ROUTE + normalUsernameToUpdate + " -- OAuth mode allows admin modification -- 201 OK" ) {
663+ test(" PATCH /orgs/" + TESTORGS (0 ).orgId + ROUTE + normalUsernameToUpdate + " -- OAuth mode allows admin modification for local user -- 201 OK" ) {
619664 withOauthEnabled {
620665 val requestBody : PatchUsersRequest = PatchUsersRequest (
621666 password = None ,
@@ -635,6 +680,26 @@ class TestPatchUserRoute extends AnyFunSuite with BeforeAndAfterAll with BeforeA
635680 }
636681 }
637682
683+ test(" PATCH /orgs/" + TESTORGS (0 ).orgId + ROUTE + TESTUSERS (5 ).username + " -- OAuth mode allows admin modification for external user -- 201 OK" ) {
684+ withOauthEnabled {
685+ val requestBody : PatchUsersRequest = PatchUsersRequest (
686+ password = None ,
687+ admin = Some (true ),
688+ hubAdmin = None ,
689+ email = None
690+ )
691+ val response : HttpResponse [String ] = Http (URL + TESTORGS (0 ).orgId + ROUTE + TESTUSERS (5 ).username).postData(Serialization .write(requestBody)).method(" PATCH" ).headers(ACCEPT ).headers(CONTENT ).headers(ORG1ADMINAUTH ).asString
692+ info(" Code: " + response.code)
693+ info(" Body: " + response.body)
694+ assert(response.code === HttpCode .POST_OK .intValue)
695+
696+ val updatedUser : UserRow = Await .result(DBCONNECTION .run(UsersTQ .filter(_.user === TESTUSERS (5 ).user).result), AWAITDURATION ).head
697+ assert(updatedUser.isOrgAdmin === true )
698+ assert(updatedUser.email === TESTUSERS (5 ).email) // Should remain unchanged
699+ assert(updatedUser.password === TESTUSERS (5 ).password) // Should remain unchanged
700+ }
701+ }
702+
638703 test(" PATCH /orgs/root" + ROUTE + " TestPatchUserRouteHubAdmin -- OAuth mode allows hubAdmin modification -- 201 OK" ) {
639704 withOauthEnabled {
640705 val requestBody : PatchUsersRequest = PatchUsersRequest (
@@ -672,5 +737,5 @@ class TestPatchUserRoute extends AnyFunSuite with BeforeAndAfterAll with BeforeA
672737 assertNoChanges(TESTUSERS (2 ))
673738 }
674739 }
675-
740+
676741}
0 commit comments