Skip to content

Commit

Permalink
Moved authorisation config spa to component based spa (#5571)
Browse files Browse the repository at this point in the history
*  #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
kritika-singh3 authored and bdpiprava committed Dec 19, 2018
1 parent 9a38940 commit f07be85
Show file tree
Hide file tree
Showing 19 changed files with 1,353 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class Toggles {
public static String AGENT_APIS_OVER_RAILS = "agent_apis_over_rails";
public static String SERVER_DRAIN_MODE_API_TOGGLE_KEY = "server_drain_mode_api_toggle_key";
public static String USE_OLD_ELASTIC_PROFILE_SPA = "use_old_elastic_profile_spa";
public static String USE_NEW_AUTH_CONFIG_SPA = "use_new_auth_config_spa";

private static FeatureToggleService service;

Expand Down
5 changes: 5 additions & 0 deletions server/src/main/resources/available.toggles
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
"key": "use_old_elastic_profile_spa",
"description": "Switch elastic profile spa to use old spa.",
"value": false
},
{
"key": "use_new_auth_config_spa",
"description": "Switch authorization configuration spa to use new spa.",
"value": false
}
]
}
12 changes: 12 additions & 0 deletions server/webapp/WEB-INF/rails/webpack/helpers/spark_routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,16 @@ export default class {
static cancelStage(pipelineName: string, stageName: string): string {
return `/go/api/stages/${pipelineName}/${stageName}/cancel`;
}

static authConfigPath(authConfigId?: string): string {
if (authConfigId) {
return `/go/api/admin/security/auth_configs/${authConfigId}`;
} else {
return "/go/api/admin/security/auth_configs";
}
}

static adminInternalVerifyConnectionPath(): string {
return `/go/api/admin/internal/security/auth_configs/verify_connection`;
}
}
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>;
});
};
}
}
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));
}
}
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()))
};
}
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"]);
});
});

0 comments on commit f07be85

Please sign in to comment.