-
Notifications
You must be signed in to change notification settings - Fork 973
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved authorisation config spa to component based spa (#5571)
* #5552 - Initial commit to move auth config spa to component based spa * #5552 - Listing auth configs using collapsable panel * #5552 Added toggle to switch between old and new auth config spa * #5552 added check connection functionality * stubbed ajax for auth config spa * Removing page test will write functional test for it. * Added CRUD spec.
- Loading branch information
1 parent
9a38940
commit f07be85
Showing
19 changed files
with
1,353 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
server/webapp/WEB-INF/rails/webpack/models/auth_configs/auth_configs_crud.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2018 ThoughtWorks, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import {ApiRequestBuilder, ApiResult, ApiVersion, ObjectWithEtag} from "helpers/api_request_builder"; | ||
import SparkRoutes from "helpers/spark_routes"; | ||
import {AuthConfig, AuthConfigJSON, AuthConfigs} from "models/auth_configs/auth_configs_new"; | ||
|
||
export class AuthConfigsCRUD { | ||
private static API_VERSION_HEADER = ApiVersion.v1; | ||
|
||
static all() { | ||
return ApiRequestBuilder.GET(SparkRoutes.authConfigPath(), this.API_VERSION_HEADER) | ||
.then((result: ApiResult<string>) => result.map((body) => { | ||
return AuthConfigs.fromJSON(JSON.parse(body)); | ||
})); | ||
} | ||
|
||
static get(id: string) { | ||
return ApiRequestBuilder.GET(SparkRoutes.authConfigPath(id), this.API_VERSION_HEADER) | ||
.then(this.extractObjectWithEtag()); | ||
} | ||
|
||
static update(updatedAuthConfig: AuthConfig, etag: string) { | ||
return ApiRequestBuilder.PUT(SparkRoutes.authConfigPath(updatedAuthConfig.id()), | ||
this.API_VERSION_HEADER, | ||
{payload: updatedAuthConfig, etag}) | ||
.then(this.extractObjectWithEtag()); | ||
} | ||
|
||
static create(authConfig: AuthConfig) { | ||
return ApiRequestBuilder.POST(SparkRoutes.authConfigPath(), | ||
this.API_VERSION_HEADER, | ||
{payload: authConfig}).then(this.extractObjectWithEtag()); | ||
} | ||
|
||
static delete(id: string) { | ||
return ApiRequestBuilder.DELETE(SparkRoutes.authConfigPath(id), this.API_VERSION_HEADER) | ||
.then((result: ApiResult<string>) => result.map((body) => JSON.parse(body))); | ||
} | ||
|
||
static verifyConnection(authConfig: AuthConfig) { | ||
return ApiRequestBuilder.POST(SparkRoutes.adminInternalVerifyConnectionPath(), | ||
this.API_VERSION_HEADER, | ||
{payload: authConfig}) | ||
.then((result: ApiResult<string>) => { | ||
return result.map((body) => JSON.parse(body)); | ||
}); | ||
} | ||
|
||
private static extractObjectWithEtag() { | ||
return (result: ApiResult<string>) => { | ||
return result.map((body) => { | ||
const authConfigJSON = JSON.parse(body) as AuthConfigJSON; | ||
return { | ||
object: AuthConfig.fromJSON(authConfigJSON), | ||
etag: result.getEtag() | ||
} as ObjectWithEtag<AuthConfig>; | ||
}); | ||
}; | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
server/webapp/WEB-INF/rails/webpack/models/auth_configs/auth_configs_new.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright 2018 ThoughtWorks, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import {Stream} from "mithril/stream"; | ||
import * as stream from "mithril/stream"; | ||
import {Errors} from "models/mixins/errors"; | ||
import {applyMixins} from "models/mixins/mixins"; | ||
import {ValidatableMixin} from "models/mixins/new_validatable_mixin"; | ||
import {Configurations, PropertyJSON} from "models/shared/configuration"; | ||
|
||
export interface AuthConfigJSON { | ||
id: string; | ||
plugin_id: string; | ||
properties: PropertyJSON[]; | ||
errors?: { [key: string]: string[] }; | ||
} | ||
|
||
interface EmbeddedJSON { | ||
auth_configs: AuthConfigJSON[]; | ||
} | ||
|
||
export interface AuthConfigsJSON { | ||
_embedded: EmbeddedJSON; | ||
} | ||
|
||
export class AuthConfig extends ValidatableMixin { | ||
id: Stream<string>; | ||
pluginId: Stream<string>; | ||
properties: Stream<Configurations>; | ||
|
||
constructor(id?: string, pluginId?: string, properties?: Configurations, errors?: Errors) { | ||
super(); | ||
this.id = stream(id); | ||
this.pluginId = stream(pluginId); | ||
this.properties = stream(properties); | ||
this.errors(errors); | ||
|
||
ValidatableMixin.call(this); | ||
this.validatePresenceOf("pluginId"); | ||
this.validatePresenceOf("id"); | ||
this.validateFormatOf("id", | ||
new RegExp("^[-a-zA-Z0-9_][-a-zA-Z0-9_.]*$"), | ||
{message: "Invalid Id. This must be alphanumeric and can contain underscores and periods (however, it cannot start with a period)."}); | ||
this.validateMaxLength("id", 255, {message: "The maximum allowed length is 255 characters."}); | ||
} | ||
|
||
static fromJSON(data: AuthConfigJSON) { | ||
const configurations = Configurations.fromJSON(data.properties); | ||
const errors = new Errors(data.errors); | ||
return new AuthConfig(data.id, data.plugin_id, configurations, errors); | ||
} | ||
|
||
toJSON(): object { | ||
return { | ||
id: this.id, | ||
plugin_id: this.pluginId, | ||
properties: this.properties | ||
}; | ||
} | ||
} | ||
|
||
applyMixins(AuthConfig, ValidatableMixin); | ||
|
||
export class AuthConfigs extends Array<AuthConfig> { | ||
constructor(...authConfigs: AuthConfig[]) { | ||
super(...authConfigs); | ||
} | ||
|
||
static fromJSON(data: AuthConfigsJSON) { | ||
return new AuthConfigs(...data._embedded.auth_configs.map(AuthConfig.fromJSON)); | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
server/webapp/WEB-INF/rails/webpack/models/auth_configs/spec/auth_configs_crud_spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* Copyright 2018 ThoughtWorks, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import {AuthConfigsCRUD} from "models/auth_configs/auth_configs_crud"; | ||
import {AuthConfig, AuthConfigJSON} from "models/auth_configs/auth_configs_new"; | ||
import {TestData} from "models/auth_configs/spec/test_data"; | ||
|
||
describe("AuthorizationConfigurationCRUD", () => { | ||
beforeEach(() => jasmine.Ajax.install()); | ||
afterEach(() => jasmine.Ajax.uninstall()); | ||
|
||
it("should make get request", () => { | ||
jasmine.Ajax.stubRequest("/go/api/admin/security/auth_configs").andReturn(authConfigResponse()); | ||
|
||
AuthConfigsCRUD.all(); | ||
|
||
const request = jasmine.Ajax.requests.mostRecent(); | ||
expect(request.url).toEqual("/go/api/admin/security/auth_configs"); | ||
expect(request.method).toEqual("GET"); | ||
expect(request.data()).toEqual(toJSON({} as AuthConfigJSON)); | ||
expect(request.requestHeaders).toEqual({Accept: "application/vnd.go.cd.v1+json"}); | ||
}); | ||
|
||
it("should create a new auth config", () => { | ||
jasmine.Ajax.stubRequest("/go/api/admin/security/auth_configs").andReturn(authConfigResponse()); | ||
|
||
AuthConfigsCRUD.create(AuthConfig.fromJSON(TestData.ldapAuthConfig())); | ||
|
||
const request = jasmine.Ajax.requests.mostRecent(); | ||
expect(request.url).toEqual("/go/api/admin/security/auth_configs"); | ||
expect(request.method).toEqual("POST"); | ||
expect(request.data()).toEqual(toJSON(TestData.ldapAuthConfig())); | ||
expect(request.requestHeaders).toEqual({ | ||
"Accept": "application/vnd.go.cd.v1+json", | ||
"Content-Type": "application/json; charset=utf-8" | ||
}); | ||
|
||
}); | ||
|
||
it("should update a auth config", () => { | ||
const ldapAuthConfig = TestData.ldapAuthConfig(); | ||
jasmine.Ajax.stubRequest(`/go/api/admin/security/auth_configs/${ldapAuthConfig.id}`) | ||
.andReturn(authConfigResponse()); | ||
|
||
AuthConfigsCRUD.update(AuthConfig.fromJSON(ldapAuthConfig), "some-etag"); | ||
|
||
const request = jasmine.Ajax.requests.mostRecent(); | ||
expect(request.url).toEqual(`/go/api/admin/security/auth_configs/${ldapAuthConfig.id}`); | ||
expect(request.method).toEqual("PUT"); | ||
expect(request.data()).toEqual(toJSON(ldapAuthConfig)); | ||
expect(request.requestHeaders).toEqual({ | ||
"Accept": "application/vnd.go.cd.v1+json", | ||
"Content-Type": "application/json; charset=utf-8", | ||
"If-Match": "some-etag" | ||
}); | ||
|
||
}); | ||
|
||
it("should delete a auth config", () => { | ||
const ldapAuthConfig = TestData.ldapAuthConfig(); | ||
jasmine.Ajax.stubRequest(`/go/api/admin/security/auth_configs/${ldapAuthConfig.id}`) | ||
.andReturn(authConfigResponse()); | ||
|
||
AuthConfigsCRUD.delete(ldapAuthConfig.id); | ||
|
||
const request = jasmine.Ajax.requests.mostRecent(); | ||
expect(request.url).toEqual(`/go/api/admin/security/auth_configs/${ldapAuthConfig.id}`); | ||
expect(request.method).toEqual("DELETE"); | ||
expect(request.data()).toEqual(toJSON({} as AuthConfigJSON)); | ||
expect(request.requestHeaders).toEqual({ | ||
"Accept": "application/vnd.go.cd.v1+json", | ||
"Content-Type": "application/json; charset=utf-8", | ||
"X-GoCD-Confirm": "true" | ||
}); | ||
|
||
}); | ||
|
||
}); | ||
|
||
function toJSON(object: any) { | ||
return JSON.parse(JSON.stringify(object)); | ||
} | ||
|
||
function authConfigResponse() { | ||
return { | ||
status: 200, | ||
responseHeaders: { | ||
"Content-Type": "application/vnd.go.cd.v1+json; charset=utf-8", | ||
"ETag": "some-etag" | ||
}, | ||
responseText: JSON.stringify(TestData.authConfigList(TestData.ldapAuthConfig())) | ||
}; | ||
} |
89 changes: 89 additions & 0 deletions
89
server/webapp/WEB-INF/rails/webpack/models/auth_configs/spec/auth_configs_spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright 2018 ThoughtWorks, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import {TestData} from "models/auth_configs/spec/test_data"; | ||
import {AuthConfigs} from "../auth_configs_new"; | ||
|
||
describe("AuthorizationConfigurationModel", () => { | ||
it("should deserialize json to AuthConfigs", () => { | ||
const authConfigsJSON = TestData.authConfigList(TestData.ldapAuthConfig(), TestData.gitHubAuthConfig()); | ||
const authConfigs = AuthConfigs.fromJSON(authConfigsJSON); | ||
|
||
expect(authConfigs.length).toEqual(2); | ||
|
||
expect(authConfigs[0].id()).toEqual("ldap"); | ||
expect(authConfigs[0].pluginId()).toEqual("cd.go.authorization.ldap"); | ||
expect(authConfigs[0].properties().count()).toEqual(3); | ||
expect(authConfigs[0].properties().valueFor("Url")).toEqual("ldap://ldap.server.url"); | ||
expect(authConfigs[0].properties().valueFor("ManagerDN")).toEqual("uid=admin,ou=system"); | ||
expect(authConfigs[0].properties().valueFor("Password")).toEqual("gGx7G+4+BAQ="); | ||
|
||
expect(authConfigs[1].id()).toEqual("github"); | ||
expect(authConfigs[1].pluginId()).toEqual("cd.go.authorization.github"); | ||
expect(authConfigs[1].properties().count()).toEqual(3); | ||
expect(authConfigs[1].properties().valueFor("Url")).toEqual("https://foo.github.com"); | ||
expect(authConfigs[1].properties().valueFor("ClientKey")).toEqual("some-key"); | ||
expect(authConfigs[1].properties().valueFor("ClientSecret")).toEqual("gGx7G+4+BAQ="); | ||
}); | ||
|
||
it("should validate presence of plugin id", () => { | ||
const ldapAuthConfigJSON = TestData.ldapAuthConfig(); | ||
delete ldapAuthConfigJSON.plugin_id; | ||
const authConfigs = AuthConfigs.fromJSON(TestData.authConfigList(ldapAuthConfigJSON)); | ||
|
||
const isValid = authConfigs[0].isValid(); | ||
|
||
expect(isValid).toBe(false); | ||
expect(authConfigs[0].errors().count()).toEqual(1); | ||
expect(authConfigs[0].errors().keys()).toEqual(["pluginId"]); | ||
}); | ||
|
||
it("should validate presence of id", () => { | ||
const ldapAuthConfigJSON = TestData.ldapAuthConfig(); | ||
delete ldapAuthConfigJSON.id; | ||
const authConfigs = AuthConfigs.fromJSON(TestData.authConfigList(ldapAuthConfigJSON)); | ||
|
||
const isValid = authConfigs[0].isValid(); | ||
|
||
expect(isValid).toBe(false); | ||
expect(authConfigs[0].errors().count()).toEqual(1); | ||
expect(authConfigs[0].errors().keys()).toEqual(["id"]); | ||
}); | ||
|
||
it("should validate pattern for id", () => { | ||
const ldapAuthConfigJSON = TestData.ldapAuthConfig(); | ||
ldapAuthConfigJSON.id = "&%$Not-allowed"; | ||
const authConfigs = AuthConfigs.fromJSON(TestData.authConfigList(ldapAuthConfigJSON)); | ||
|
||
const isValid = authConfigs[0].isValid(); | ||
|
||
expect(isValid).toBe(false); | ||
expect(authConfigs[0].errors().count()).toEqual(1); | ||
expect(authConfigs[0].errors().keys()).toEqual(["id"]); | ||
}); | ||
|
||
it("should validate length for id", () => { | ||
const ldapAuthConfigJSON = TestData.ldapAuthConfig(); | ||
ldapAuthConfigJSON.id = "This-is-longer-then-255-characters_This-is-longer-then-255-characters_This-is-longer-then-255-characters_This-is-longer-then-255-characters_This-is-longer-then-255-characters_This-is-longer-then-255-characters_This-is-longer-then-255-characters_This-is-longer-then-255-characters"; | ||
const authConfigs = AuthConfigs.fromJSON(TestData.authConfigList(ldapAuthConfigJSON)); | ||
|
||
const isValid = authConfigs[0].isValid(); | ||
|
||
expect(isValid).toBe(false); | ||
expect(authConfigs[0].errors().count()).toEqual(1); | ||
expect(authConfigs[0].errors().keys()).toEqual(["id"]); | ||
}); | ||
}); |
Oops, something went wrong.