/*GRB*

    Gerbera - https://gerbera.io/

    login.spec.js - this file is part of Gerbera.

    Copyright (C) 2016-2025 Gerbera Contributors

    Gerbera is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 2
    as published by the Free Software Foundation.

    Gerbera is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Gerbera.  If not, see <http://www.gnu.org/licenses/>.

    $Id$
*/

const { expect } = require('chai');

const TestUtils = require('./page/test-utils');
const LoginPage = require('./page/login.page');

let driver;

describe('Login Suite', () => {
  let loginPage;

  before(async () => {
    driver = await TestUtils.newChromeDriver();
    await TestUtils.resetSuite('login.test.requests.json', driver);
    loginPage = new LoginPage(driver);
  });

  after(() => driver && driver.quit());

  describe('The login action', () => {

    beforeEach(async () => {
      await TestUtils.resetCookies(driver);
      await loginPage.get(TestUtils.home());
    });

    it('hides the login form when no authentication is required', async () => {
      const form = await loginPage.loginForm();
      const style = await form.getAttribute('style');
      expect(style).to.equal('display: none;');
    });

    it('shows the login form when no session exists yet and accounts is required', async () => {
      const form = await loginPage.loginForm();
      const style = await form.getAttribute('style');
      expect(style).to.equal('');
    });

    it('requires user name and password to submit the login form', async () => {
      await loginPage.password('');
      await loginPage.username('');
      await loginPage.submitLogin();
      await driver.sleep(1000);

      let result = await loginPage.getToastMessage();
      expect(result).to.equal('Please enter username and password');

      await loginPage.closeToast();
    });

    it('shows logout button, and shows form on logout when successful login', async () => {
      await loginPage.password('pwd');
      await loginPage.username('user');
      await loginPage.submitLogin();
      await loginPage.logout();

      const form = await loginPage.loginForm();
      const style = await form.getAttribute('style');
      expect(style).to.equal('');
    });

    it('hides menu, hides login and shows message when UI is disabled', async () => {
      let result = await loginPage.getToastMessage();
      expect(result).to.equal('The UI is disabled in the configuration file. See README.');
      await loginPage.closeToast();

      const form = await loginPage.loginForm();
      expect(await form.getAttribute('style')).to.equal('display: none;');

      const sid = await loginPage.getCookie('GerberaSID');
      expect(sid).to.be.null;
    });

    it('reloads the page and lets user login again when session expires', async () => {
      await driver.sleep(1000); // allow fields to load

      const form = await loginPage.loginForm();
      const style = await form.getAttribute('style');
      expect(style, "login form to to be visible").to.equal('');

      const result = await loginPage.loginButtonIsDisplayed();
      expect(result, 'Login Button should be displayed').to.be.true;
    });
  });
});
