# 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/>.


import responses

from . import WebCase


class IdentityHandlerTest(WebCase):

    login = True

    def test_get_img_with_picture(self):
        # Given a user with an image.
        self.responses.add(
            responses.GET,
            "https://img.bfh.ch/?id=test-user&size=small",
            json={},
            status=200,
        )
        # When querying the user's image
        self.getPage("/dashboard/img?u=test-user")
        # Then an image is returned
        self.assertStatus(200)

    def test_get_img_without_picture(self):
        # Given a user without image
        self.responses.add(
            responses.GET,
            "https://img.bfh.ch/?id=test-user&size=small",
            status=404,
        )
        # When querying the user's image
        self.getPage("/dashboard/img?u=test-user")
        # Then redirect to placeholder
        self.assertStatus(303)
        self.assertHeaderItemValue('Location', f'{self.baseurl}/static/portrait-placeholder.png')

    def test_identity_card(self):
        # Given a user
        self.conn.strategy.add_entry(
            'cn=test-academic-title,ou=Groups,dc=bfh',
            {
                'bfhAcademicTitle': 'Dr.',
                'bfhAccountType': 'Staff',
                'bfhMailAlias': 'jordan@test.com',
                'bfhSection': 'Fachbereich Agronomie',
                'bfhUPN': 'jor1@bfh.ch',
                'gidNumber': 10088,
                'givenName': 'Jorven',
                'l': 'Montréal',
                'mail': 'jordan.marks82@example.com',
                'o': 'HAFL',
                'objectClass': ['person', 'organizationalPerson', 'inetOrgPerson', 'posixAccount'],
                'ou': 'AGR',
                'postalCode': 'H3Y 2K7',
                'preferredLanguage': 'de',
                'sn': 'Brackley',
                'street': '3 Willowbend Drive',
                'telephoneNumber': '(437) 592-8143',
                'title': 'Lecturer',
                'uid': ['test-academic-title'],
                'uidNumber': 115338341,
                'userPassword': self.password,
            },
        )
        self._login('test-academic-title', self.password)
        # When querying the user's profile
        self.getPage("/dashboard/")
        self.assertStatus(200)
        # Then identity card is populated
        self.assertInBody('Dr. Jorven Brackley')
        self.assertInBody('German')
        self.assertInBody('jordan.marks82@example.com')
        self.assertInBody('jordan@test.com')
        self.assertInBody('(437) 592-8143')
        self.assertInBody('(437) 592-8143')
        self.assertInBody('Lecturer')
        self.assertInBody('Fachbereich Agronomie')
        self.assertInBody('AGR')
        self.assertInBody('HAFL')
        self.assertInBody('3 Willowbend Drive, H3Y 2K7 Montréal')
        self.assertInBody('115338341 / 10088')
        self.assertInBody('jor1@bfh.ch')
        self.assertInBody('cn=test-academic-title,ou=Groups,dc=bfh')
