{"size": 2771, "ext": "js", "max_stars_repo_path": "test/os/auth.test.js", "max_stars_repo_name": "dpew/opensphere", "max_stars_repo_stars_event_min_datetime": "2017-12-19T15:16:08.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-17T21:09:14.000Z", "max_issues_repo_path": "test/os/auth.test.js", "max_issues_repo_name": "dpew/opensphere", "max_issues_repo_issues_event_min_datetime": "2017-12-21T10:01:49.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-24T13:02:47.000Z", "max_forks_repo_path": "test/os/auth.test.js", "max_forks_repo_name": "dpew/opensphere", "max_forks_repo_forks_event_min_datetime": "2017-12-18T01:51:06.000Z", "max_forks_repo_forks_event_max_datetime": "2021-12-06T14:18:27.000Z"}
{"max_stars_count": 170, "max_issues_count": 571, "max_forks_count": 90, "avg_line_length": 34.6375, "max_line_length": 88, "alphanum_fraction": 0.6669072537}
goog.require('os.alert.AlertManager');
goog.require('os.auth');
goog.require('os.config.Settings');
goog.require('os.data.RecordField');
goog.require('os.mock');


describe('plugin.imagery.menu', () => {
  const {
    SettingKey,
    initAuth,
    getAuth,
    alertAuth
  } = goog.module.get('os.auth');
  const {default: Settings} = goog.module.get('os.config.Settings');
  const {default: AlertManager} = goog.module.get('os.alert.AlertManager');

  const settingsAuth = {
    'test': {
      'title': 'My Test Auth',
      'tooltip': 'Test Tooltip',
      'message': 'Go here to authenticate with the test server.',
      'pattern': '(http://my.test.url|Test Name)',
      'link': 'http://my.test.url/login'
    }
  };

  it('should initialize from settings', () => {
    // check before adding anything to settings
    let testAuth = getAuth('http://my.test.url');
    expect(testAuth).toBe(null);

    // add the test object and check that we get something
    Settings.getInstance().set(SettingKey, settingsAuth);
    initAuth();

    testAuth = getAuth('http://my.test.url');
    expect(testAuth.title).toBe('My Test Auth');
    expect(testAuth.tooltip).toBe('Test Tooltip');
    expect(testAuth.message).toBe('Go here to authenticate with the test server.');
    expect(testAuth.pattern instanceof RegExp).toBe(true);
    expect(testAuth.link).toBe('http://my.test.url/login');
  });

  it('should get auth objects from settings by URL or name', () => {
    Settings.getInstance().set(SettingKey, settingsAuth);
    initAuth();

    testAuth = getAuth('http://my.test.url');
    expect(testAuth.title).toBe('My Test Auth');
    expect(testAuth.tooltip).toBe('Test Tooltip');
    expect(testAuth.message).toBe('Go here to authenticate with the test server.');
    expect(testAuth.pattern instanceof RegExp).toBe(true);
    expect(testAuth.link).toBe('http://my.test.url/login');

    testAuth = null;

    // test it with a name
    testAuth = getAuth('Test Name');
    expect(testAuth.title).toBe('My Test Auth');
    expect(testAuth.tooltip).toBe('Test Tooltip');
    expect(testAuth.message).toBe('Go here to authenticate with the test server.');
    expect(testAuth.pattern instanceof RegExp).toBe(true);
    expect(testAuth.link).toBe('http://my.test.url/login');
  });

  it('should send alerts exactly once per auth entry', () => {
    Settings.getInstance().set(SettingKey, settingsAuth);
    initAuth();
    spyOn(AlertManager.getInstance(), 'sendAlert').andCallThrough();

    // verify that we attempt to fire an alert
    alertAuth('http://my.test.url');

    const calls = AlertManager.getInstance().sendAlert.calls;
    expect(calls.length).toBe(1);
    expect(calls[0].args[0]).toContain(`Go here to authenticate with the test server.`);
  });
});
