Commit a05c542e authored by Leonardo Menezes's avatar Leonardo Menezes
Browse files

added tests to connect controller

parent 5a0f16cb
Loading
Loading
Loading
Loading
+13 −20
Original line number Diff line number Diff line
@@ -653,33 +653,26 @@ angular.module('cerebro').controller('ConnectController', [

    $scope.connecting = false;

    $scope.host = undefined;

    $scope.username = undefined;

    $scope.password = undefined;

    $scope.showAuth = false;

    $scope.setup = function() {
      DataService.getHosts(
        function(hosts) {
          $scope.hosts = hosts;
        },
        function(error) {

          AlertService.error('Error while fetching list of known hosts', error);
        }
      );
    };

    $scope.connect = function(host) {
    $scope.connect = function(host, username, password) {
      if (host) {
        $scope.connecting = true;
        DataService.setHost(
          host,
          $scope.username,
          $scope.password,
          username,
          password,
          function(response) {
            $location.path('/overview');
            $scope.host = DataService.getHost();
          },
          function(response) {
            $scope.connecting = false;
+30 −31
Original line number Diff line number Diff line
@@ -8,41 +8,39 @@
        </h4>
      </div>
    </div>
    <div>
      <label>Known hosts</label>
      <table class="table table-bordered table-condensed table-rounded shard-map">
    <div style="padding-top: 60px;">
      <h6>HOSTS</h6>
      <table class="table">
        <tr ng-repeat="host in hosts track by $index">
          <td class="normal-action" ng-click="connect(host)">
            <span>{{host}}</span>
          </td>
        </tr>
      </table>
      <div class="form-group row">
      <form>
        <div class="row">
          <div class="col-xs-12">
          <label for="newHost">Connect to a new host</label>
            <input id="newHost" type="text" ng-model="host" class="form-control form-control-sm"
                 placeholder="example: http://localhost:9200">
                   placeholder="example: http://localhost:9200" ng-enter="connect(host, username, password)">
          </div>
        </div>
      <div class="form-group row">
        <div class="row form-group">
          <div class="col-xs-12 text-right">
          <span ng-hide="showAuth" ng-click="showAuth = true" data-toggle="collapse" data-target="#collapseAuth">
            <small>Authentication</small>
            <i class="fa fa-angle-down" ></i>
          </span>
          <span ng-show="showAuth" ng-click="showAuth = false" data-toggle="collapse" data-target="#collapseAuth">
          <span ng-click="showAuth = !showAuth" data-toggle="collapse" data-target="#collapseAuth"
                class="normal-action">
            <small>Authentication</small>
            <i class="fa fa-angle-up"></i>
            <i class="fa" ng-class="{'fa-angle-down': !showAuth, 'fa-angle-up': showAuth}"></i>
          </span>
            <div class="collapse text-left" id="collapseAuth">
              <div class="form-group">
                <label for="username">Username</label>
                <input id="username" type="text" ng-model="username" class="form-control form-control-sm"
                     placeholder="admin">
                       placeholder="admin" ng-enter="connect(host, username, password)">
              </div>
              <div class="form-group">
                <label for="password">Password</label>
              <input id="password" type="text" ng-model="password" class="form-control form-control-sm">
                <input id="password" type="text" ng-model="password" class="form-control form-control-sm"
                       ng-enter="connect(host, username, password)">
              </div>
            </div>
          </div>
@@ -52,9 +50,10 @@
                <span class="pull-left subtitle" ng-show="connecting">
                    <i class="fa fa-fw fa-circle-o-notch fa-spin"> </i> Connecting
                </span>
          <button type="button" class="btn btn-success pull-right" ng-click="connect(host)">Connect</button>
            <button type="submit" class="btn btn-success pull-right" ng-click="connect(host, username, password)">Connect</button>
          </div>
        </div>
      </form>
    </div>
  </div>
</div>
+13 −20
Original line number Diff line number Diff line
@@ -6,33 +6,26 @@ angular.module('cerebro').controller('ConnectController', [

    $scope.connecting = false;

    $scope.host = undefined;

    $scope.username = undefined;

    $scope.password = undefined;

    $scope.showAuth = false;

    $scope.setup = function() {
      DataService.getHosts(
        function(hosts) {
          $scope.hosts = hosts;
        },
        function(error) {

          AlertService.error('Error while fetching list of known hosts', error);
        }
      );
    };

    $scope.connect = function(host) {
    $scope.connect = function(host, username, password) {
      if (host) {
        $scope.connecting = true;
        DataService.setHost(
          host,
          $scope.username,
          $scope.password,
          username,
          password,
          function(response) {
            $location.path('/overview');
            $scope.host = DataService.getHost();
          },
          function(response) {
            $scope.connecting = false;
+103 −0
Original line number Diff line number Diff line
describe('ConnectController', function() {

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

  beforeEach(angular.mock.inject(function($rootScope, $controller, $injector) {
    this.scope = $rootScope.$new();
    this.$location = $injector.get('$location');
    this.DataService = $injector.get('DataService');
    this.AlertService = $injector.get('AlertService');
    this.createController = function() {
      return $controller('ConnectController',
        {$scope: this.scope}, this.$location, this.DataService, this.AlertService);
    };
    this._controller = this.createController();
  }));

  it('should have intial state correctly set', function() {
    expect(this.scope.hosts).toEqual(undefined);
    expect(this.scope.connecting).toEqual(false);
  });

  describe('setup', function() {
    it('initializes list of known hosts editor', function() {
      var hosts = {host: 'http://somehost'};
      this.DataService.getHosts = function(success, error) {
        success(hosts);
      };
      spyOn(this.DataService, "getHosts").andCallThrough();
      this.scope.setup();
      expect(this.DataService.getHosts).toHaveBeenCalled();
      expect(this.scope.hosts).toEqual(hosts);
    });
    it('initializes list of known hosts editor', function() {
      var msg = 'kaput';
      this.DataService.getHosts = function(success, error) {
        error(msg);
      };
      spyOn(this.DataService, 'getHosts').andCallThrough();
      spyOn(this.AlertService, 'error').andReturn(true);
      this.scope.setup();
      expect(this.DataService.getHosts).toHaveBeenCalled();
      expect(this.scope.hosts).toEqual(undefined);
      expect(this.AlertService.error).toHaveBeenCalledWith('Error while fetching list of known hosts', 'kaput');
    });
  });

  describe('connect', function() {
    it('connects to valid host', function() {
      this.DataService.setHost = function(host, username, password, success, error) {
        success();
      };
      spyOn(this.DataService, "setHost").andCallThrough();
      spyOn(this.$location, 'path').andReturn(true);
      this.scope.connect('http://localhost:9200');
      expect(this.DataService.setHost).toHaveBeenCalledWith(
        'http://localhost:9200',
        undefined,
        undefined,
        jasmine.any(Function),
        jasmine.any(Function)
      );
      expect(this.$location.path).toHaveBeenCalledWith('/overview');
      expect(this.scope.connecting).toEqual(true);
    });

    it('connects to valid host passing username/password', function() {
      this.DataService.setHost = function(host, username, password, success, error) {
        success();
      };
      spyOn(this.DataService, "setHost").andCallThrough();
      spyOn(this.$location, 'path').andReturn(true);
      this.scope.connect('http://localhost:9200', 'admin', '1234');
      expect(this.DataService.setHost).toHaveBeenCalledWith(
        'http://localhost:9200',
        'admin',
        '1234',
        jasmine.any(Function),
        jasmine.any(Function)
      );
      expect(this.$location.path).toHaveBeenCalledWith('/overview');
    });

    it('fails attempting to connect', function() {
      this.DataService.setHost = function(host, username, password, success, error) {
        error();
      };
      spyOn(this.DataService, "setHost").andCallThrough();
      spyOn(this.AlertService, "error").andReturn();
      this.scope.connect('http://localhost:9200', 'admin', '1234');
      expect(this.DataService.setHost).toHaveBeenCalledWith(
        'http://localhost:9200',
        'admin',
        '1234',
        jasmine.any(Function),
        jasmine.any(Function)
      );
      expect(this.scope.connecting).toEqual(false);
      expect(this.AlertService.error).toHaveBeenCalledWith('Error connecting to http://localhost:9200', undefined);
    });

  })

});