# BFH Science Self-Service Portal
# Copyright (C) 2025-2026 IKUS Software
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 this program.  If not, see <https://www.gnu.org/licenses/>.


from . import WebCase


class IdentityHandlerTest(WebCase):
    default_config = {
        "ldap-firstname-attribute": "givenName",
        "ldap-lastname-attribute": "sn",
        "ldap-email-attribute": "mail",
    }
    login = False

    def test_get_login_page(self):
        # Given a user not authenticated
        # When querying the login page
        self.getPage('/login/')
        # Then page get shown
        self.assertStatus(200)
        self.assertInBody('Login')
        self.assertInBody('Password')

    def test_redirect_to_login(self):
        # Given a user not authenticated
        # When querying the dashboard page
        self.getPage('/dashboard/')
        # Then user is redirected to login page
        self.assertStatus(303)
        self.assertHeaderItemValue('Location', f'{self.baseurl}/login/')

    def test_login_wrong_password(self):
        # Given a user not authenticated
        # When trying to login with wrong password.
        self.getPage("/login/", method='POST', body={'login': 'invalid-user', 'password': 'my-password'})
        # Then login page contains warning
        self.assertStatus(200)
        self.assertInBody('Invalid login or password.')
        self.assertInBody('invalid-user')
        self.assertNotInBody('my-password')
