/**
 * Copyright (C) 2019  The Software Heritage developers
 * See the AUTHORS file at the top-level directory of this distribution
 * License: GNU Affero General Public License version 3, or any later version
 * See top-level LICENSE file for more information
 */

const username = 'admin';
const password = 'admin';
const defaultRedirect = '/admin/origin/save/';

let url;

function login(username, password) {
  cy.get('input[name="username"]')
    .type(username)
    .get('input[name="password"]')
    .type(password)
    .get('form')
    .submit();
}

function logout() {
  cy.contains('a', 'logout')
    .click();
}

describe('Test Admin Features', function() {
  before(function() {
    url = this.Urls.admin();
  });

  it('should redirect to default page', function() {
    cy.visit(url);
    login(username, password);

    cy.location('pathname')
      .should('be.equal', defaultRedirect);

    logout();
  });

  it('should display admin-origin-save and deposit in sidebar', function() {
    cy.visit(url);
    login(username, password);

    cy.get(`.sidebar a[href="${this.Urls.admin_origin_save()}"]`)
      .should('be.visible');

    cy.get(`.sidebar a[href="${this.Urls.admin_deposit()}"]`)
      .should('be.visible');

    logout();
  });

  it('should display username on top-right', function() {
    cy.visit(url);
    login(username, password);

    cy.get('.swh-position-right')
      .should('contain', username);

    logout();
  });

  it('should prevent unauthorized access after logout', function() {
    cy.visit(this.Urls.admin_origin_save())
      .location('pathname')
      .should('be.equal', '/admin/login/');
    cy.visit(this.Urls.admin_deposit())
      .location('pathname')
      .should('be.equal', '/admin/login/');
  });

  it('should redirect to correct page after login', function() {
    cy.visit(this.Urls.admin_deposit())
      .location('search')
      .should('contain', `next=${this.Urls.admin_deposit()}`);

    login(username, password);

    cy.location('pathname')
      .should('be.equal', this.Urls.admin_deposit());

    logout();
  });
});

@@ -1817,32 +1817,350 @@
 ', function() %7B%0A
+    // mock calls to deposit list api to avoid possible errors%0A    // while running the test%0A    cy.server();%0A    cy.route(%7B%0A      method: 'GET',%0A      url: %60$%7Bthis.Urls.admin_deposit_list()%7D**%60,%0A      response: %7B%0A        data: %5B%5D,%0A        recordsTotal: 0,%0A        recordsFiltered: 0,%0A        draw: 1%0A      %7D%0A    %7D);%0A%0A
     cy.visit(thi

Fix a test that sometimes fail in the admin test suite