Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CHE-1087: [dashboard] improve workspace name exists error message #1151

Merged
merged 1 commit into from
Apr 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ export class CreateProjectCtrl {
this.projectName = null;
this.projectDescription = null;
this.defaultWorkspaceName = null;

cheAPI.cheWorkspace.getWorkspaces();
}

/**
Expand Down
2 changes: 2 additions & 0 deletions dashboard/src/app/projects/create-project/create-project.html
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
che-place-holder="Name of the workspace"
ng-model="createProjectCtrl.workspaceName"
required
unique-workspace-name="createProjectCtrl.workspaceName"
ng-minlength="3"
ng-maxlength="20"
ng-pattern="/^[A-Za-z0-9_\-\.]+$/">
Expand All @@ -178,6 +179,7 @@
</div>
<div ng-message="minlength">The name has to be more then 3 characters long.</div>
<div ng-message="maxlength">The name has to be less than 20 characters long.</div>
<div ng-message="uniqueWorkspaceName">This workspace name is already used.</div>
</che-input>
</che-label-container>
<che-workspace-ram-allocation-slider ng-model="createProjectCtrl.workspaceRam"></che-workspace-ram-allocation-slider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ export class CreateWorkspaceCtrl {
this.recipeScript = null;
this.importWorkspace = '';
this.defaultWorkspaceName = null;

cheAPI.cheWorkspace.fetchWorkspaces();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
che-place-holder="Name of the workspace"
ng-model="createWorkspaceCtrl.workspaceName"
required
unique-workspace-name="createWorkspaceCtrl.workspaceName"
ng-minlength="3"
ng-maxlength="20"
ng-pattern="/^[A-Za-z0-9_\-\.]+$/">
Expand All @@ -53,6 +54,7 @@
</div>
<div ng-message="minlength">The name has to be more then 3 characters long.</div>
<div ng-message="maxlength">The name has to be less than 20 characters long.</div>
<div ng-message="uniqueWorkspaceName">This workspace name is already used.</div>
</che-input>
<che-workspace-ram-allocation-slider ng-model="createWorkspaceCtrl.workspaceRam"></che-workspace-ram-allocation-slider>
</che-panel>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2015-2016 Codenvy, S.A.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Codenvy, S.A. - initial API and implementation
*/
'use strict';

/**
* Defines a directive for checking if the workspace name is not already taken
* @author Oleksii Kurinnyi
*/
export class UniqueWorkspaceNameValidator {

/**
* Default constructor that is using resource
* @ngInject for Dependency injection
*/
constructor (cheAPI, $q) {
this.cheAPI = cheAPI;
this.$q = $q;
this.restrict='A';
this.require = 'ngModel';
}

/**
* Check that the name of workspace is unique
*/
link($scope, element, attributes, ngModel) {

// validate only input element
if ('input' === element[0].localName) {

ngModel.$asyncValidators.uniqueWorkspaceName = (modelValue) => {

// create promise
var deferred = this.$q.defer();

let workspaces = this.cheAPI.getWorkspace().getWorkspaces();
if (workspaces.length) {

for (let i=0; i<workspaces.length; i++) {
if (workspaces[i].config.name === modelValue) {
deferred.reject(false);
}
}
deferred.resolve(true);
} else {
// no workspaces so it's ok
deferred.resolve(true);
}

// return promise
return deferred.promise;
};
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Copyright (c) 2015-2016 Codenvy, S.A.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Codenvy, S.A. - initial API and implementation
*/
'use strict';

/**
* Test the workspace name uniqueness
* @author Oleksii Kurinnyi
*/

describe('unique-workspace-name-validator', function() {
var $scope, form, $compiler;

/**
* Workspace API
*/
var factoryWorkspace;

/**
* API builder.
*/
var apiBuilder;

/**
* Backend for handling http operations
*/
var httpBackend;

/**
* Che backend
*/
var cheBackend;


beforeEach(angular.mock.module('userDashboard'));


beforeEach(inject(function($compile, $rootScope, cheWorkspace, cheAPIBuilder, cheHttpBackend, $document) {
$scope = $rootScope;
$compiler = $compile;
factoryWorkspace = cheWorkspace;
apiBuilder = cheAPIBuilder;
cheBackend = cheHttpBackend;
httpBackend = cheHttpBackend.getHttpBackend();
this.$document = $document;

}));

describe('Validate Workspace Name', function() {

it('workspaceAlready exists', function() {

// setup tests objects
var idWorkspace1 = 'idOfMyWorkspace1';
var nameWorkspace1 = 'testWorkspace1';
var workspace1 = apiBuilder.getWorkspaceBuilder().withName(nameWorkspace1).withId(idWorkspace1).build();

factoryWorkspace.fetchWorkspaces();

// add into backend
cheBackend.addWorkspaces([workspace1]);
cheBackend.setup();

// flush HTTP backend
httpBackend.flush();

$scope.model = { workspaceName: null};

var element = angular.element(
'<form name="form">' +
'<input ng-model="model.workspaceName" name="name" unique-workspace-name="model.workspaceName" />' +
'</form>'
);
$compiler(element)($scope);
form = $scope.form;

form.name.$setViewValue(nameWorkspace1);

// check form (expect invalid)
expect(form.name.$invalid).toBe(true);
expect(form.name.$valid).toBe(false);
});

it('workspace not yet defined', function() {

// setup tests objects
var idWorkspace1 = 'idOfMyWorkspace1';
var nameWorkspace1 = 'testWorkspace1';
var workspace1 = apiBuilder.getWorkspaceBuilder().withName(nameWorkspace1).withId(idWorkspace1).build();

factoryWorkspace.fetchWorkspaces();

// add into backend
cheBackend.addWorkspaces([workspace1]);

// setup backend
cheBackend.setup();

// flush HTTP backend
httpBackend.flush();

$scope.model = { workspaceName: null };

var element = angular.element(
'<form name="form">' +
'<input ng-model="model.workspaceName" name="name" unique-workspace-name="model.workspaceName" />' +
'</form>'
);
$compiler(element)($scope);
form = $scope.form;

form.name.$setViewValue('dummyWorkspace');

// check form valid
expect(form.name.$invalid).toBe(false);
expect(form.name.$valid).toBe(true);

});
});
});
4 changes: 3 additions & 1 deletion dashboard/src/components/validator/validator-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@

import {GitUrlValidator} from './git-url-validator.directive';
import {UniqueProjectNameValidator} from './unique-project-name-validator.directive';
import {UniqueWorkspaceNameValidator} from './unique-workspace-name-validator.directive';


export class ValidatorConfig {

constructor(register) {

register.directive('gitUrl', GitUrlValidator)
.directive('uniqueProjectName', UniqueProjectNameValidator);
.directive('uniqueProjectName', UniqueProjectNameValidator)
.directive('uniqueWorkspaceName', UniqueWorkspaceNameValidator);

}
}