<commit_before>'use strict'
/* global sinon:false */

describe('Controller: ChannelsModalCtrl', function () {
  // load the controller's module
  beforeEach(module('openhimConsoleApp'))

  // setup config constant to be used for API server details
  beforeEach(function () {
    module('openhimConsoleApp', function ($provide) {
      $provide.constant('config', { 'protocol': 'https', 'host': 'localhost', 'port': 8080, 'title': 'Title', 'footerTitle': 'FooterTitle', 'footerPoweredBy': 'FooterPoweredBy' })
    })
  })

  var scope, createController, createControllerRoutes, httpBackend

  // Initialize the controller and a mock scope
  beforeEach(inject(function ($controller, $rootScope, $httpBackend) {
    httpBackend = $httpBackend

    $httpBackend.when('GET', new RegExp('.*/users')).respond([
      { 'firstname': 'Super', 'surname': 'User', 'email': '<EMAIL>', 'passwordAlgorithm': 'sample/<PASSWORD>', 'passwordHash': '<PASSWORD>', 'passwordSalt': '<PASSWORD>', 'groups': ['admin'] },
      { 'firstname': 'Ordinary', 'surname': 'User', 'email': '<EMAIL>', 'passwordAlgorithm': 'sample/<PASSWORD>', 'passwordHash': '<PASSWORD>', 'passwordSalt': '<PASSWORD>', 'groups': ['limited'] }
    ])

    $httpBackend.when('GET', new RegExp('.*/groups')).respond([
      { 'group': 'Group 1', 'users': [ {'user': 'User 1', 'method': 'sms', 'maxAlerts': 'no max'}, {'user': 'User 2', 'method': 'email', 'maxAlerts': '1 per day'}, {'user': 'User 3', 'method': 'email', 'maxAlerts': '1 per hour'} ] },
      { 'group': 'Group 2', 'users': [ {'user': 'User 4', 'method': 'email', 'maxAlerts': 'no max'} ] }
    ])

    // http request used in routes controller
    $httpBackend.when('GET', new RegExp('.*/mediators')).respond([
      {
        'urn': 'AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE',
        'version': '0.0.1',
        'name': 'Test 1 Mediator',
        'description': 'Test 1 Description',
        'defaultChannelConfig': [
          { 'name': 'Mediator Channel 1', 'urlPattern': '/channel1', 'routes': [{ 'name': 'Route 1', 'host': 'localhost', 'port': '1111', 'primary': true, 'type': 'http' }], 'allow': [ 'xdlab' ], 'type': 'http' }
        ],
        'endpoints': [{ 'name': 'Route 1', 'host': 'localhost', 'port': '1111', 'primary': true, 'type': 'http' }]
      }, {
        'urn': 'EEEEEEEE-DDDD-CCCC-BBBB-AAAAAAAAAAAA',
        'version': '0.1.2',
        'name': 'Test 2 Mediator',
        'description': 'Test 2 Description',
        'defaultChannelConfig': [
          { 'name': 'Mediator Channel 2', 'urlPattern': '/channnel2', 'routes': [{ 'name': 'Route', 'host': 'localhost', 'port': '2222', 'primary': true, 'type': 'http' }], 'allow': [ 'xdlab' ], 'type': 'http' }
        ],
        'endpoints': [{ 'name': 'Route', 'host': 'localhost', 'port': '2222', 'primary': true, 'type': 'http' }, { 'name': 'Route 2', 'host': 'localhost2', 'port': '3333', 'primary': false, 'type': 'http' }]
      }
    ])

    // http request used in routes controller
    $httpBackend.when('GET', new RegExp('.*/keystore/ca')).respond([
      {'country': 'US', 'state': 'Missouri', 'locality': 'St. Louis', 'organization': 'Mallinckrodt Institute of Radiology', 'organizationUnit': 'Electronic Radiology Lab', 'commonName': 'MIR2014-16', 'emailAddress': 'moult<EMAIL>', 'data': '-----FAKE CERTIFICATE DATA-----', '_id': '54e1ca5afa069b5a7b938c4f', 'validity': { 'start': '2014-10-09T13:15:28.000Z', 'end': '2016-11-29T13:15:28.000Z' }},
      {'country': 'ZA', 'state': 'KZN', 'locality': 'Durban', 'organization': 'Jembi Health Systems NPC', 'organizationUnit': 'eHealth', 'commonName': 'openhim', 'emailAddress': '<EMAIL>', 'data': '-----FAKE CERTIFICATE DATA-----', '_id': '54e1ca5afa069b5a7b938c50', 'validity': { 'start': '2014-11-25T12:52:21.000Z', 'end': '2016-10-30T12:52:21.000Z' }}
    ])

    $httpBackend.when('GET', new RegExp('.*/channels/.+/audits$')).respond([])
    $httpBackend.when('GET', new RegExp('.*/channels/.+')).respond({})

    scope = $rootScope.$new()
    var modalInstance = sinon.spy()

    createController = function (channel, channelDuplicate) {
      return $controller('ChannelsModalCtrl', {
        $scope: scope,
        $uibModalInstance: modalInstance,
        channel: channel,
        channelDuplicate: channelDuplicate,
        tab: null
      })
    }
    createControllerRoutes = function () {
      return $controller('channelRoutesCtrl', {
        $scope: scope,
        $uibModalInstance: modalInstance,
        channel: null
      })
    }
  }))

  afterEach(function () {
    httpBackend.verifyNoOutstandingExpectation()
    httpBackend.verifyNoOutstandingRequest()
  })

  it('should fetch channel data from the API when updating', function () {
    httpBackend.expect('GET', new RegExp('.*/channels/.+'))
    createController({ _id: 'test' })
    httpBackend.flush()
  })

  it('should create a new channel if this is not an update', function () {
    createController()
    httpBackend.flush()

    scope.channel.should.be.ok()
  })

  it('should create a duplicate channel from an existing channel', function () {
    createController(null, 'test')
    httpBackend.flush()

    scope.channel.should.be.ok()
    scope.channel.should.not.have.property('_id')
    scope.channel.should.not.have.property('name')
  })

  it('should run validateFormChannels() for any validation errors - ngErrors.hasErrors -> TRUE', function () {
    createController()
    // set child route controller for broadcast response
    createControllerRoutes()
    httpBackend.flush()

    scope.channel.type = 'http'
    scope.channel.authType = 'private'
    scope.matching.showRequestMatching = true
    scope.channel.name = ''
    scope.channel.urlPattern = ''
    scope.channel.allow = []
    scope.matching.contentMatching = 'XML matching'
    scope.channel.matchContentXpath = ''
    scope.channel.matchContentValue = ''
    scope.channel.routes = []

    // run the validate
    scope.validateFormChannels()

    scope.ngError.should.have.property('name', true)
    scope.ngError.should.have.property('urlPattern', true)
    scope.ngError.should.have.property('allow', true)
    scope.ngError.should.have.property('matchContentXpath', true)
    scope.ngError.should.have.property('matchContentXpath', true)
    scope.ngError.should.have.property('hasRouteWarnings', true)
  })

  it('should run validateFormChannels() for any validation errors - ngErrors.hasErrors -> FALSE', function () {
    createController()
    httpBackend.flush()

    scope.channel.name = 'ChannelName'
    scope.channel.urlPattern = 'sample/api'
    scope.channel.allow = ['allow1', 'allow2']
    scope.matching.contentMatching = 'XML matching'
    scope.channel.matchContentXpath = 'XPath'
    scope.channel.matchContentValue = 'Value'
    scope.channel.routes = [{'name': 'testRoute', 'host': 'localhost', 'port': '80', 'path': '/sample/api', 'primary': true}]

    // run the validate
    scope.validateFormChannels()
    scope.ngError.should.have.property('hasErrors', false)
  })

  it('should run submitFormChannels() and check any validation errors - FALSE - should not save the record', function () {
    createController()
    // set child route controller for broadcast response
    createControllerRoutes()
    httpBackend.flush()

    scope.channel.type = 'http'
    scope.channel.authType = 'private'
    scope.matching.showRequestMatching = true
    scope.channel.name = ''
    scope.channel.urlPattern = ''
    scope.channel.allow = []
    scope.matching.contentMatching = 'XML matching'
    scope.channel.matchContentXpath = ''
    scope.channel.matchContentValue = ''
    scope.channel.routes = []

    // run the submit
    scope.submitFormChannels()
    scope.ngError.should.have.property('name', true)
    scope.ngError.should.have.property('urlPattern', true)
    scope.ngError.should.have.property('allow', true)
    scope.ngError.should.have.property('matchContentXpath', true)
    scope.ngError.should.have.property('matchContentXpath', true)
    scope.ngError.should.have.property('hasRouteWarnings', true)
  })

  it('should run submitFormChannels() and check any validation errors - TRUE - Should save the record', function () {
    createController()
    httpBackend.flush()

    scope.channel.$save = sinon.spy()

    // update is false so create new channel
    scope.update = false

    scope.channel.name = 'ChannelName'
    scope.channel.urlPattern = 'sample/api'
    scope.channel.allow = ['allow1', 'allow2']
    scope.matching.contentMatching = 'XML matching'
    scope.channel.matchContentXpath = 'XPath'
    scope.channel.matchContentValue = 'Value'
    scope.channel.routes = [{'name': 'testRoute', 'host': 'localhost', 'port': '80', 'path': '/sample/api', 'primary': true}]
    // run the submit
    scope.submitFormChannels()
    scope.ngError.should.have.property('hasErrors', false)

    scope.channel.$save.should.have.been.called()
  })

  it('should run submitFormChannels() and add the regex delimiters to the URL Pattern', function () {
    createController()
    httpBackend.flush()

    scope.channel.$save = sinon.spy()

    // update is false so create new channel
    scope.update = false

    scope.channel.name = 'ChannelName'
    scope.channel.urlPattern = 'sample/api'
    scope.urlPattern.regex = true
    scope.channel.allow = ['allow1', 'allow2']
    scope.matching.contentMatching = 'XML matching'
    scope.channel.matchContentXpath = 'XPath'
    scope.channel.matchContentValue = 'Value'
    scope.channel.routes = [{'name': 'testRoute', 'host': 'localhost', 'port': '80', 'path': '/sample/api', 'primary': true}]
    // run the submit
    scope.submitFormChannels()
    scope.ngError.should.have.property('hasErrors', false)
    scope.channel.should.have.property('urlPattern', '^sample/api$')
    scope.channel.$save.should.have.been.called()
  })

  it('should run submitFormChannels() and check any validation errors - TRUE - Should update the record', function () {
    createController()
    httpBackend.flush()

    scope.channel.$update = sinon.spy()

    // update is false so create new channel
    scope.update = true

    scope.channel.type = 'http'
    scope.channel.authType = 'private'
    scope.matching.showRequestMatching = true
    scope.channel.name = 'ChannelName'
    scope.channel.urlPattern = 'sample/api'
    scope.urlPattern.regex = false
    scope.channel.allow = ['allow1', 'allow2']
    scope.matching.contentMatching = 'XML matching'
    scope.channel.matchContentXpath = 'XPath'
    scope.channel.matchContentValue = 'Value'
    scope.channel.routes = [{'name': 'testRoute', 'host': 'localhost', 'port': '80', 'path': '/sample/api', 'primary': true}]

    // run the submit
    scope.submitFormChannels()
    scope.ngError.should.have.property('hasErrors', false)
    scope.channel.$update.should.have.been.called()

    scope.channel.should.have.property('name', 'ChannelName')
    scope.channel.should.have.property('urlPattern', 'sample/api')
    scope.channel.should.have.property('matchContentXpath', 'XPath')
    scope.channel.should.have.property('matchContentValue', 'Value')
    scope.channel.allow.should.have.length(2)
    scope.channel.routes.should.have.length(1)
  })
})

describe('Controller: channelBasicInfoCtrl', function () {
  // load the controller's module
  beforeEach(module('openhimConsoleApp'))<commit_msg>Add a test for channel methods<commit_after>'use strict'
/* global sinon:false */

describe('Controller: ChannelsModalCtrl', function () {
  // load the controller's module
  beforeEach(module('openhimConsoleApp'))

  // setup config constant to be used for API server details
  beforeEach(function () {
    module('openhimConsoleApp', function ($provide) {
      $provide.constant('config', { 'protocol': 'https', 'host': 'localhost', 'port': 8080, 'title': 'Title', 'footerTitle': 'FooterTitle', 'footerPoweredBy': 'FooterPoweredBy' })
    })
  })

  var scope, createController, createControllerRoutes, httpBackend

  // Initialize the controller and a mock scope
  beforeEach(inject(function ($controller, $rootScope, $httpBackend) {
    httpBackend = $httpBackend

    $httpBackend.when('GET', new RegExp('.*/users')).respond([
      { 'firstname': 'Super', 'surname': 'User', 'email': '<EMAIL>', 'passwordAlgorithm': 'sample/api', 'passwordHash': '<PASSWORD>', 'passwordSalt': '<PASSWORD>', 'groups': ['admin'] },
      { 'firstname': 'Ordinary', 'surname': 'User', 'email': '<EMAIL>', 'passwordAlgorithm': 'sample/api', 'passwordHash': '<PASSWORD>', 'passwordSalt': '79b01b37ff62', 'groups': ['limited'] }
    ])

    $httpBackend.when('GET', new RegExp('.*/groups')).respond([
      { 'group': 'Group 1', 'users': [ {'user': 'User 1', 'method': 'sms', 'maxAlerts': 'no max'}, {'user': 'User 2', 'method': 'email', 'maxAlerts': '1 per day'}, {'user': 'User 3', 'method': 'email', 'maxAlerts': '1 per hour'} ] },
      { 'group': 'Group 2', 'users': [ {'user': 'User 4', 'method': 'email', 'maxAlerts': 'no max'} ] }
    ])

    // http request used in routes controller
    $httpBackend.when('GET', new RegExp('.*/mediators')).respond([
      {
        'urn': 'AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE',
        'version': '0.0.1',
        'name': 'Test 1 Mediator',
        'description': 'Test 1 Description',
        'defaultChannelConfig': [
          { 'name': 'Mediator Channel 1', 'urlPattern': '/channel1', 'routes': [{ 'name': 'Route 1', 'host': 'localhost', 'port': '1111', 'primary': true, 'type': 'http' }], 'allow': [ 'xdlab' ], 'type': 'http' }
        ],
        'endpoints': [{ 'name': 'Route 1', 'host': 'localhost', 'port': '1111', 'primary': true, 'type': 'http' }]
      }, {
        'urn': 'EEEEEEEE-DDDD-CCCC-BBBB-AAAAAAAAAAAA',
        'version': '0.1.2',
        'name': 'Test 2 Mediator',
        'description': 'Test 2 Description',
        'defaultChannelConfig': [
          { 'name': 'Mediator Channel 2', 'urlPattern': '/channnel2', 'routes': [{ 'name': 'Route', 'host': 'localhost', 'port': '2222', 'primary': true, 'type': 'http' }], 'allow': [ 'xdlab' ], 'type': 'http' }
        ],
        'endpoints': [{ 'name': 'Route', 'host': 'localhost', 'port': '2222', 'primary': true, 'type': 'http' }, { 'name': 'Route 2', 'host': 'localhost2', 'port': '3333', 'primary': false, 'type': 'http' }]
      }
    ])

    // http request used in routes controller
    $httpBackend.when('GET', new RegExp('.*/keystore/ca')).respond([
      {'country': 'US', 'state': 'Missouri', 'locality': 'St. Louis', 'organization': 'Mallinckrodt Institute of Radiology', 'organizationUnit': 'Electronic Radiology Lab', 'commonName': 'MIR2014-16', 'emailAddress': '<EMAIL>', 'data': '-----FAKE CERTIFICATE DATA-----', '_id': '54e1ca5afa069b5a7b938c4f', 'validity': { 'start': '2014-10-09T13:15:28.000Z', 'end': '2016-11-29T13:15:28.000Z' }},
      {'country': 'ZA', 'state': 'KZN', 'locality': 'Durban', 'organization': 'Jembi Health Systems NPC', 'organizationUnit': 'eHealth', 'commonName': 'openhim', 'emailAddress': '<EMAIL>', 'data': '-----FAKE CERTIFICATE DATA-----', '_id': '54e1ca5afa069b5a7b938c50', 'validity': { 'start': '2014-11-25T12:52:21.000Z', 'end': '2016-10-30T12:52:21.000Z' }}
    ])

    $httpBackend.when('GET', new RegExp('.*/channels/.+/audits$')).respond([])
    $httpBackend.when('GET', new RegExp('.*/channels/.+')).respond({})

    scope = $rootScope.$new()
    var modalInstance = sinon.spy()

    createController = function (channel, channelDuplicate) {
      return $controller('ChannelsModalCtrl', {
        $scope: scope,
        $uibModalInstance: modalInstance,
        channel: channel,
        channelDuplicate: channelDuplicate,
        tab: null
      })
    }
    createControllerRoutes = function () {
      return $controller('channelRoutesCtrl', {
        $scope: scope,
        $uibModalInstance: modalInstance,
        channel: null
      })
    }
  }))

  afterEach(function () {
    httpBackend.verifyNoOutstandingExpectation()
    httpBackend.verifyNoOutstandingRequest()
  })

  it('should fetch channel data from the API when updating', function () {
    httpBackend.expect('GET', new RegExp('.*/channels/.+'))
    createController({ _id: 'test' })
    httpBackend.flush()
  })

  it('should create a new channel if this is not an update', function () {
    createController()
    httpBackend.flush()

    scope.channel.should.be.ok()
  })

  it('should create a duplicate channel from an existing channel', function () {
    createController(null, 'test')
    httpBackend.flush()

    scope.channel.should.be.ok()
    scope.channel.should.not.have.property('_id')
    scope.channel.should.not.have.property('name')
  })

  it('should run validateFormChannels() for any validation errors - ngErrors.hasErrors -> TRUE', function () {
    createController()
    // set child route controller for broadcast response
    createControllerRoutes()
    httpBackend.flush()

    scope.channel.type = 'http'
    scope.channel.authType = 'private'
    scope.matching.showRequestMatching = true
    scope.channel.name = ''
    scope.channel.urlPattern = ''
    scope.channel.allow = []
    scope.matching.contentMatching = 'XML matching'
    scope.channel.matchContentXpath = ''
    scope.channel.matchContentValue = ''
    scope.channel.routes = []

    // run the validate
    scope.validateFormChannels()

    scope.ngError.should.have.property('name', true)
    scope.ngError.should.have.property('urlPattern', true)
    scope.ngError.should.have.property('allow', true)
    scope.ngError.should.have.property('matchContentXpath', true)
    scope.ngError.should.have.property('matchContentXpath', true)
    scope.ngError.should.have.property('hasRouteWarnings', true)
  })

  it('should run validateFormChannels() for any validation errors - ngErrors.hasErrors -> FALSE', function () {
    createController()
    httpBackend.flush()

    scope.channel.name = 'ChannelName'
    scope.channel.urlPattern = 'sample/api'
    scope.channel.allow = ['allow1', 'allow2']
    scope.matching.contentMatching = 'XML matching'
    scope.channel.matchContentXpath = 'XPath'
    scope.channel.matchContentValue = 'Value'
    scope.channel.routes = [{'name': 'testRoute', 'host': 'localhost', 'port': '80', 'path': '/sample/api', 'primary': true}]

    // run the validate
    scope.validateFormChannels()
    scope.ngError.should.have.property('hasErrors', false)
  })

  it('should run submitFormChannels() and check any validation errors - FALSE - should not save the record', function () {
    createController()
    // set child route controller for broadcast response
    createControllerRoutes()
    httpBackend.flush()

    scope.channel.type = 'http'
    scope.channel.authType = 'private'
    scope.matching.showRequestMatching = true
    scope.channel.name = ''
    scope.channel.urlPattern = ''
    scope.channel.allow = []
    scope.matching.contentMatching = 'XML matching'
    scope.channel.matchContentXpath = ''
    scope.channel.matchContentValue = ''
    scope.channel.routes = []

    // run the submit
    scope.submitFormChannels()
    scope.ngError.should.have.property('name', true)
    scope.ngError.should.have.property('urlPattern', true)
    scope.ngError.should.have.property('allow', true)
    scope.ngError.should.have.property('matchContentXpath', true)
    scope.ngError.should.have.property('matchContentXpath', true)
    scope.ngError.should.have.property('hasRouteWarnings', true)
  })

  it('should run submitFormChannels() and check any validation errors - TRUE - Should save the record', function () {
    createController()
    httpBackend.flush()

    scope.channel.$save = sinon.spy()

    // update is false so create new channel
    scope.update = false

    scope.channel.name = 'ChannelName'
    scope.channel.urlPattern = 'sample/api'
    scope.channel.allow = ['allow1', 'allow2']
    scope.matching.contentMatching = 'XML matching'
    scope.channel.matchContentXpath = 'XPath'
    scope.channel.matchContentValue = 'Value'
    scope.channel.routes = [{'name': 'testRoute', 'host': 'localhost', 'port': '80', 'path': '/sample/api', 'primary': true}]
    // run the submit
    scope.submitFormChannels()
    scope.ngError.should.have.property('hasErrors', false)

    scope.channel.$save.should.have.been.called()
  })

  it('should run submitFormChannels() and add the regex delimiters to the URL Pattern', function () {
    createController()
    httpBackend.flush()

    scope.channel.$save = sinon.spy()

    // update is false so create new channel
    scope.update = false

    scope.channel.name = 'ChannelName'
    scope.channel.urlPattern = 'sample/api'
    scope.urlPattern.regex = true
    scope.channel.allow = ['allow1', 'allow2']
    scope.matching.contentMatching = 'XML matching'
    scope.channel.matchContentXpath = 'XPath'
    scope.channel.matchContentValue = 'Value'
    scope.channel.routes = [{'name': 'testRoute', 'host': 'localhost', 'port': '80', 'path': '/sample/api', 'primary': true}]
    // run the submit
    scope.submitFormChannels()
    scope.ngError.should.have.property('hasErrors', false)
    scope.channel.should.have.property('urlPattern', '^sample/api$')
    scope.channel.$save.should.have.been.called()
  })

  it('should run submitFormChannels() and check any validation errors - TRUE - Should update the record', function () {
    createController()
    httpBackend.flush()

    scope.channel.$update = sinon.spy()

    // update is false so create new channel
    scope.update = true

    scope.channel.type = 'http'
    scope.channel.authType = 'private'
    scope.matching.showRequestMatching = true
    scope.channel.name = 'ChannelName'
    scope.channel.urlPattern = 'sample/api'
    scope.urlPattern.regex = false
    scope.channel.allow = ['allow1', 'allow2']
    scope.matching.contentMatching = 'XML matching'
    scope.channel.matchContentXpath = 'XPath'
    scope.channel.matchContentValue = 'Value'
    scope.channel.routes = [{'name': 'testRoute', 'host': 'localhost', 'port': '80', 'path': '/sample/api', 'primary': true}]

    // run the submit
    scope.submitFormChannels()
    scope.ngError.should.have.property('hasErrors', false)
    scope.channel.$update.should.have.been.called()

    scope.channel.should.have.property('name', 'ChannelName')
    scope.channel.should.have.property('methods')
    scope.channel.methods.length.should.equal(9)
    scope.channel.should.have.property('urlPattern', 'sample/api')
    scope.channel.should.have.property('matchContentXpath', 'XPath')
    scope.channel.should.have.property('matchContentValue', 'Value')
    scope.channel.allow.should.have.length(2)
    scope.channel.routes.should.have.length(1)
  })
})

describe('Controller: channelBasicInfoCtrl', function () {
  // load the controller's module
  beforeEach(module('openhimConsoleApp'))
{'fraction_non_alphanumeric': 0.17160542605030948, 'fraction_numerical': 0.015233328943324992, 'mean_word_length': 3.798820307562671, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 8, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2218965', 'n_tokens_mistral': 7215, 'n_tokens_neox': 6963, 'n_words': 1520}