Ver Fonte

AMBARI-15264. Ambari Admin: incorrect alerts on Roles page (alexantonenko)

Alex Antonenko há 9 anos atrás
pai
commit
e347aef423

+ 10 - 1
ambari-admin/src/main/resources/ui/admin-web/app/scripts/controllers/clusters/UserAccessListCtrl.js

@@ -81,6 +81,12 @@ function($scope, $location, Cluster, $modal, $rootScope, $routeParams, Permissio
 
 
   // TODO change to PUT after it's available
   // TODO change to PUT after it's available
   $scope.save = function(user) {
   $scope.save = function(user) {
+    for (var i = $scope.roles.length; i--;) {
+      if ($scope.roles[i].permission_name === user.permission_name) {
+        user.permission_label = $scope.roles[i].permission_label;
+        break;
+      }
+    }
     Cluster.deletePrivilege(
     Cluster.deletePrivilege(
     $routeParams.id,
     $routeParams.id,
     user.privilege_id
     user.privilege_id
@@ -96,7 +102,10 @@ function($scope, $location, Cluster, $modal, $rootScope, $routeParams, Permissio
           principal_type: user.principal_type
           principal_type: user.principal_type
         }}]
         }}]
         ).then(function() {
         ).then(function() {
-          Alert.success(user.principal_name + " changed to " + user.permission_label);
+          Alert.success($t('users.alerts.roleChanged', {
+            name: user.principal_name,
+            role: user.permission_label
+          }));
           $scope.loadUsers();
           $scope.loadUsers();
         })
         })
         .catch(function(data) {
         .catch(function(data) {

+ 2 - 1
ambari-admin/src/main/resources/ui/admin-web/app/scripts/i18n.config.js

@@ -294,7 +294,8 @@ angular.module('ambariAdminConsole')
         'removeUserError': 'Removing from group error',
         'removeUserError': 'Removing from group error',
         'cannotAddUser': 'Cannot add user to group',
         'cannotAddUser': 'Cannot add user to group',
         'passwordChanged': 'Password changed.',
         'passwordChanged': 'Password changed.',
-        'cannotChangePassword': 'Cannot change password'
+        'cannotChangePassword': 'Cannot change password',
+        'roleChanged': '{{name}} changed to {{role}}'
       }
       }
     },
     },
 
 

+ 195 - 3
ambari-admin/src/main/resources/ui/admin-web/test/unit/controllers/clusters/UserAccessListCtrl_test.js

@@ -20,17 +20,37 @@ describe('#Cluster', function () {
 
 
   describe('UserAccessListCtrl', function() {
   describe('UserAccessListCtrl', function() {
 
 
-    var scope, ctrl, $t, $httpBackend;
+    var scope, ctrl, $t, $httpBackend, Cluster, deferred, Alert, mock;
 
 
     beforeEach(module('ambariAdminConsole', function () {}));
     beforeEach(module('ambariAdminConsole', function () {}));
 
 
-    beforeEach(inject(function($rootScope, $controller, _$translate_, _$httpBackend_) {
+    beforeEach(inject(function($rootScope, $controller, _$translate_, _$httpBackend_, _Cluster_, _$q_, _Alert_) {
       scope = $rootScope.$new();
       scope = $rootScope.$new();
       $t = _$translate_.instant;
       $t = _$translate_.instant;
       $httpBackend = _$httpBackend_;
       $httpBackend = _$httpBackend_;
+      Cluster = _Cluster_;
+      Alert = _Alert_;
+      deferred = {
+        deletePrivilege: _$q_.defer(),
+        createPrivileges: _$q_.defer()
+      };
       ctrl = $controller('UserAccessListCtrl', {
       ctrl = $controller('UserAccessListCtrl', {
         $scope: scope
         $scope: scope
       });
       });
+      mock = {
+        Cluster: Cluster,
+        Alert: Alert,
+        scope: scope
+      };
+      spyOn(Cluster, 'deletePrivilege').andReturn(deferred.deletePrivilege.promise);
+      spyOn(Cluster, 'createPrivileges').andReturn(deferred.createPrivileges.promise);
+      spyOn(Alert, 'success').andCallFake(angular.noop);
+      spyOn(Alert, 'error').andCallFake(angular.noop);
+      spyOn(scope, 'loadRoles').andCallFake(angular.noop);
+      $httpBackend.expectGET(/\/api\/v1\/clusters\/\w+\/privileges/).respond(200, {
+        items: []
+      });
+      $httpBackend.flush();
     }));
     }));
 
 
     describe('#clearFilters()', function () {
     describe('#clearFilters()', function () {
@@ -207,7 +227,6 @@ describe('#Cluster', function () {
 
 
       cases.forEach(function (item) {
       cases.forEach(function (item) {
         it(item.title, function () {
         it(item.title, function () {
-          $httpBackend.expectGET(/\/api\/v1\/clusters\/\w+\/privileges/).respond(200);
           scope.currentNameFilter = item.currentNameFilter;
           scope.currentNameFilter = item.currentNameFilter;
           scope.currentRoleFilter = item.currentRoleFilter;
           scope.currentRoleFilter = item.currentRoleFilter;
           scope.currentTypeFilter = item.currentTypeFilter;
           scope.currentTypeFilter = item.currentTypeFilter;
@@ -218,6 +237,179 @@ describe('#Cluster', function () {
 
 
     });
     });
 
 
+    describe('#save()', function () {
+
+      var user,
+        labelCases = [
+          {
+            roles: [],
+            label: '',
+            title: 'roles not loaded'
+          },
+          {
+            roles: [
+              {
+                permission_name: 'CLUSTER.OPERATOR',
+                permission_label: 'Cluster Operator'
+              },
+              {
+                permission_name: 'CLUSTER.ADMINISTRATOR',
+                permission_label: 'Cluster Administrator'
+              }
+            ],
+            label: '',
+            title: 'no roles match'
+          },
+          {
+            roles: [
+              {
+                permission_name: 'CLUSTER.USER',
+                permission_label: 'Cluster User'
+              },
+              {
+                permission_name: 'CLUSTER.OPERATOR',
+                permission_label: 'Cluster Operator'
+              },
+              {
+                permission_name: 'CLUSTER.ADMINISTRATOR',
+                permission_label: 'Cluster Administrator'
+              }
+            ],
+            label: 'Cluster User',
+            title: 'roles not loaded'
+          }
+        ],
+        deferredCases = [
+          {
+            success: ['deletePrivilege', 'createPrivileges'],
+            fail: [],
+            called: [
+              {
+                context: 'Cluster',
+                methodName: 'createPrivileges'
+              },
+              {
+                context: 'Alert',
+                methodName: 'success'
+              }
+            ],
+            notCalled: [
+              {
+                context: 'Alert',
+                methodName: 'error'
+              }
+            ],
+            title: 'all requests are successful'
+          },
+          {
+            success: ['deletePrivilege'],
+            fail: ['createPrivileges'],
+            called: [
+              {
+                context: 'Cluster',
+                methodName: 'createPrivileges'
+              },
+              {
+                context: 'Alert',
+                methodName: 'error'
+              }
+            ],
+            notCalled: [
+              {
+                context: 'Alert',
+                methodName: 'success'
+              }
+            ],
+            title: 'new role request failed'
+          },
+          {
+            success: [],
+            fail: ['deletePrivilege'],
+            called: [],
+            notCalled: [
+              {
+                context: 'Cluster',
+                methodName: 'createPrivileges'
+              },
+              {
+                context: 'Alert',
+                methodName: 'success'
+              },
+              {
+                context: 'Alert',
+                methodName: 'error'
+              }
+            ],
+            title: 'delete current role request failed'
+          }
+        ];
+
+      beforeEach(function () {
+        user = {
+          permission_name: 'CLUSTER.USER',
+          permission_label: ''
+        };
+      });
+
+      labelCases.forEach(function (item) {
+
+        it(item.title, function () {
+          scope.roles = item.roles;
+          scope.save(user);
+          expect(user.permission_label).toEqual(item.label);
+        });
+
+      });
+
+      deferredCases.forEach(function (item) {
+
+        describe(item.title, function () {
+
+          beforeEach(function () {
+            scope.roles = [];
+            scope.save(user);
+            item.success.forEach(function (method) {
+              deferred[method].resolve();
+            });
+            item.fail.forEach(function (method) {
+              deferred[method].reject({
+                data: {
+                  data: {}
+                }
+              });
+            });
+            $httpBackend.expectGET(/\/api\/v1\/clusters\/\w+\/privileges/).respond(200, {
+              items: []
+            });
+            scope.$digest();
+          });
+
+          it('Cluster.deletePrivileges', function () {
+            expect(Cluster.deletePrivilege).toHaveBeenCalled();
+          });
+
+          it('scope.loadRoles', function () {
+            expect(scope.loadRoles).toHaveBeenCalled();
+          });
+
+          item.called.forEach(function (method) {
+            it(method.context + '.' + method.methodName, function () {
+              expect(mock[method.context][method.methodName]).toHaveBeenCalled();
+            });
+          });
+
+          item.notCalled.forEach(function (method) {
+            it(method.context + '.' + method.methodName, function () {
+              expect(mock[method.context][method.methodName]).not.toHaveBeenCalled();
+            });
+          });
+
+        });
+
+      });
+
+    });
+
   });
   });
 
 
 });
 });