Skip to content

Commit

Permalink
fix failling tets
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesus-Osuna-M committed Feb 23, 2024
1 parent 1b9ad56 commit 9c79de9
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ class WebhooksProjectImporterSpec extends Specification {
def authContext = Mock(UserAndRolesAuthContext)
def files=['webhooks.yaml':new File(getClass().getClassLoader().getResource("webhooks.yaml").toURI())]
when:
def errors = importer.doImport(authContext, "webhook", files, [regenAuthTokens: false])
def errors = importer.doImport(authContext, "webhook", files, [:])

then:
errors.isEmpty()
2 * importer.webhookService.importWebhook(_,_,_) >> {
2 * importer.webhookService.importWebhook(_,_,_,_) >> {
[msg:"ok"]
}
}

static interface MockWebhookService {
def importWebhook(UserAndRolesAuthContext authContext, def hookData, boolean regenFlag)
def importWebhook(UserAndRolesAuthContext authContext, def hookData, boolean regenFlag,boolean regenUuid)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -354,18 +354,20 @@ class ProjectControllerSpec extends Specification implements ControllerUnitTest<
true | false | false | false | false | false
}

def "api v34 exportAll include webhooks auth tokens when whkIncludeAuthTokens is set to true"(){
def "api v34 exportAll include webhooks auth tokens when whkIncludeAuthTokens && whkRegenUuid are set to true"(){

given:"a project to be exported"
controller.projectService = Mock(ProjectService)
controller.apiService = Mock(ApiService)
controller.frameworkService = Mock(FrameworkService)
setupGetResource()
params.project = 'aproject'
Map<String, String> exportOpts = [(WebhooksProjectExporter.INLUDE_AUTH_TOKENS):"true",(WebhooksProjectExporter.WHK_REGEN_UUID):"true"]

when:"exporting the project using the API"
params.exportAll = "true"
params.whkIncludeAuthTokens = "true"
params.whkRegenUuid = "true"
request.api_version = ApiVersions.V34
controller.apiProjectExport()

Expand All @@ -374,7 +376,7 @@ class ProjectControllerSpec extends Specification implements ControllerUnitTest<
1 * controller.apiService.requireApi(_, _) >> true
1 * controller.projectService.exportProjectToOutputStream(_, _, _, _, { ArchiveOptions opts ->
opts.all == true &&
opts.exportOpts[WebhooksProjectComponent.COMPONENT_NAME]==[(WebhooksProjectExporter.INLUDE_AUTH_TOKENS):"true"]
opts.exportOpts[WebhooksProjectComponent.COMPONENT_NAME]==exportOpts
},_
)
}
Expand Down Expand Up @@ -429,10 +431,12 @@ class ProjectControllerSpec extends Specification implements ControllerUnitTest<
controller.frameworkService = Mock(FrameworkService)
setupGetResource()

Map<String, String> exportOpts = [(WebhooksProjectExporter.INLUDE_AUTH_TOKENS):whinclude.toString(),(WebhooksProjectExporter.WHK_REGEN_UUID):regenUuid.toString()]
params.project = 'aproject'

params.exportWebhooks=whenable.toString()
params.whkIncludeAuthTokens=whinclude.toString()
params.whkRegenUuid=regenUuid.toString()
request.api_version = 34

when:
Expand All @@ -443,14 +447,14 @@ class ProjectControllerSpec extends Specification implements ControllerUnitTest<

1 * controller.projectService.exportProjectToOutputStream(_, _, _, _, { ArchiveOptions opts ->
opts.exportComponents[WebhooksProjectComponent.COMPONENT_NAME] == whenable &&
opts.exportOpts[WebhooksProjectComponent.COMPONENT_NAME]==[(WebhooksProjectExporter.INLUDE_AUTH_TOKENS):whinclude.toString()]
opts.exportOpts[WebhooksProjectComponent.COMPONENT_NAME]==exportOpts
},_
)

where:
whenable | whinclude
true | true
true | false
whenable | whinclude | regenUuid
true | true | true
true | false | false
}

def "api project delete error"() {
Expand Down Expand Up @@ -2263,6 +2267,9 @@ class ProjectControllerSpec extends Specification implements ControllerUnitTest<
request.content = 'test'.bytes
params.importWebhooks='true'
params.whkRegenAuthTokens='true'
params.whkRegenUuid='true'
Map<String, String> exportOpts = [(WebhooksProjectImporter.WHK_REGEN_AUTH_TOKENS):"true",
(WebhooksProjectImporter.WHK_REGEN_UUID):"true"]
when: "import project via api"
controller.apiProjectImport(aparams)
then: "webhook component import options are set"
Expand All @@ -2274,8 +2281,7 @@ class ProjectControllerSpec extends Specification implements ControllerUnitTest<
// **Deprecated**
// 1 * controller.projectService.importToProject(project,_,_,_,{ ProjectArchiveImportRequest req->
1 * controller.projectService.handleApiImport(_,_,project,_,{ ProjectArchiveImportRequest req->
req.importComponents == [(WebhooksProjectComponent.COMPONENT_NAME): true]
req.importOpts == [(WebhooksProjectComponent.COMPONENT_NAME): [(WebhooksProjectImporter.WHK_REGEN_AUTH_TOKENS): 'true']]
req.importOpts.webhooks == exportOpts
}) >> [success:true]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1824,7 +1824,7 @@ class ProjectServiceSpec extends Specification implements ServiceUnitTest<Projec
'node-wizard' : true
],
exportOpts : [
'webhooks' : ['inludeAuthTokens' : 'true']
'webhooks' : ['inludeAuthTokens' : 'true', 'regenUuid':'true']
],
])
File archive = new File("testfile")
Expand Down Expand Up @@ -1855,7 +1855,7 @@ class ProjectServiceSpec extends Specification implements ServiceUnitTest<Projec
'node-wizard' : true
],
exportOpts : [
'webhooks' : ['inludeAuthTokens' : 'true']
'webhooks' : ['inludeAuthTokens' : 'true', 'regenUuid':'true']
],
])
File archive = new File("testfile")
Expand Down Expand Up @@ -1883,7 +1883,7 @@ class ProjectServiceSpec extends Specification implements ServiceUnitTest<Projec
'node-wizard' : true
],
exportOpts : [
'webhooks' : ['inludeAuthTokens' : 'true']
'webhooks' : ['inludeAuthTokens' : 'true', 'regenUuid':'true']
],
])
File archive = new File("testfile")
Expand Down Expand Up @@ -3305,6 +3305,7 @@ abstract class MockRundeckApi implements RundeckApi{
Boolean importScm,
Boolean importWebhooks,
Boolean whkRegenAuthTokens,
Boolean whkRegenUuid,
Boolean importNodesSources,
Map<String,String> params,
RequestBody body
Expand All @@ -3322,6 +3323,6 @@ abstract class MockRundeckApi implements RundeckApi{
response = Response.error(responseStatusCode, errorResponseJsonBody)
}

return delegate.returning(Calls.response(response)).importProjectArchive(project,jobUuidOption, importExecutions, importConfig, importACL, importScm, importWebhooks, whkRegenAuthTokens, importNodesSources, params, body)
return delegate.returning(Calls.response(response)).importProjectArchive(project,jobUuidOption, importExecutions, importConfig, importACL, importScm, importWebhooks, whkRegenAuthTokens,whkRegenUuid, importNodesSources, params, body)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -321,20 +321,30 @@ class WebhookServiceSpec extends Specification implements ServiceUnitTest<Webhoo
1 * service.rundeckAuthTokenManagerService.deleteByTokenWithType('12345', AuthTokenType.WEBHOOK )

}
def "webhook name must be unique in project"() {
def "webhook name could be used multiples times"() {
given:
def mockUserAuth = Mock(UserAndRolesAuthContext) {
getUsername() >> { "webhookUser" }
getRoles() >> { ["webhook","test"] }
}
Webhook existing = new Webhook(name:"test",project: "Test",authToken: "12345",eventPlugin: "log-webhook-event")
existing.save()
service.userService = Mock(MockUserService) {
validateUserExists(_) >> { true }
}
service.apiService = Mock(MockApiService)
service.pluginService = Mock(MockPluginService) {
validatePluginConfig(_,_,_) >> { return new ValidatedPlugin(report: new Validator.Report(),valid:true) }
getPlugin(_,_) >> { new TestWebhookEventPlugin() }
listPlugins(WebhookEventPlugin) >> { ["log-webhook-event":new TestWebhookEventPlugin()] }
}

when:
def result = service.saveHook(mockUserAuth,[name:"test",project:"Test",user:"webhookUser",roles:"webhook,test",eventPlugin:"log-webhook-event","config":["cfg1":"val1"]])

then:
result == [err:"A Webhook by that name already exists in this project"]
_ * service.apiService.generateUserToken(_,_,_,_,_,_) >> { [token:"12345"] }
result.msg == "Saved webhook"

}

Expand Down Expand Up @@ -429,6 +439,7 @@ class WebhookServiceSpec extends Specification implements ServiceUnitTest<Webhoo
@Unroll
def "import webhooks regenAuthToken: #regenFlag"() {
given:
def uuid = "0dfb6080-935e-413d-a6a7-cdee9345cf72"
service.pluginService = Mock(MockPluginService) {
listPlugins(_) >> { ["log-webhook-event":new Object()]}
}
Expand All @@ -441,10 +452,10 @@ class WebhookServiceSpec extends Specification implements ServiceUnitTest<Webhoo

when:
def result = service.importWebhook(authContext,[name:"test",
uuid: "0dfb6080-935e-413d-a6a7-cdee9345cf72",
uuid: uuid,
project:"Test", authToken:'abc123', user:'webhookUser', roles:"webhook,test",
eventPlugin:"log-webhook-event",
config:'{}'],regenFlag)
config:'{}'],regenFlag,regenUuid)
Webhook created = Webhook.findByName("test")

then:
Expand All @@ -456,11 +467,13 @@ class WebhookServiceSpec extends Specification implements ServiceUnitTest<Webhoo
1 * service.rundeckAuthTokenManagerService.parseAuthRoles("webhook,test") >> { new HashSet(['webhook','test']) }
expectGenTokenCall * service.apiService.generateUserToken(_,_,_,_,_,_) >> { [token:"12345"] }
expectImportWhkToken * service.rundeckAuthTokenManagerService.importWebhookToken(authContext,'abc123','webhookUser',new HashSet(['webhook','test'])) >> { true }

if(regenUuid){
created.uuid != uuid
}
where:
regenFlag | expectGenTokenCall | expectImportWhkToken
true | 1 | 0
false | 0 | 1
regenFlag | expectGenTokenCall | expectImportWhkToken | regenUuid
true | 1 | 0 | true
false | 0 | 1 | false
}

def "import is allowed"() {
Expand Down Expand Up @@ -597,7 +610,7 @@ class WebhookServiceSpec extends Specification implements ServiceUnitTest<Webhoo
whPersisted.name == "test-change-name"
}

def "Cannot import a webhook with the same name and different uuid in the same project"() {
def "import a webhook with the same name and different uuid in the same project"() {
given:
def mockUserAuth = Mock(UserAndRolesAuthContext) {
getUsername() >> { "webhookUser" }
Expand All @@ -619,11 +632,10 @@ class WebhookServiceSpec extends Specification implements ServiceUnitTest<Webhoo
existing.save()

when:
def result = service.importWebhook(mockUserAuth, [id: 1, uuid: "d1c6dcf7-dd12-4858-9373-c12639c689d4", name: "test", project: "Test", authToken: "12345", eventPlugin: "log-webhook-event"], false)

def result = service.importWebhook(mockUserAuth, [id: 1, uuid: "d1c6dcf7-dd12-4858-9373-c12639c689d4", name: "test", project: "Test", authToken: "12345", eventPlugin: "log-webhook-event"], true,false)
then:
result == [err:"Unable to import webhoook test. Error:A Webhook by that name already exists in this project"]

result.msg == "Webhook test imported"
1 * service.apiService.generateUserToken(_,_,_,_,_,_) >> { [token:"12345"] }
}

def "Cannot import a webhook with existing token"() {
Expand All @@ -648,8 +660,7 @@ class WebhookServiceSpec extends Specification implements ServiceUnitTest<Webhoo
existing.save()

when:"import data with same token into project B"
def result = service.importWebhook(mockUserAuth, imported, false)

def result = service.importWebhook(mockUserAuth, imported, false,false)
then:"should fail"
result == [err:'Unable to import webhoook test. Error:Cannot import webhook: imported auth token does not exist or was changed']
where:
Expand Down Expand Up @@ -683,7 +694,7 @@ class WebhookServiceSpec extends Specification implements ServiceUnitTest<Webhoo


when:"import data with same token into project A"
def result = service.importWebhook(mockUserAuth, definition, false)
def result = service.importWebhook(mockUserAuth, definition, false,false)


then:"should have 1 webhook"
Expand Down

0 comments on commit 9c79de9

Please sign in to comment.