# 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 InfraHandlerTest(WebCase):

    login = True

    def test_infra_json_empty(self):
        # Given a user without data
        # When querying storage list
        self.getJson("/dashboard/infra.json")
        # Then the list is empty.
        self.assertBody('{"data": []}')

    def test_infra_json_with_data(self):
        # Given a user with data
        self.conn.strategy.add_entry(
            'cn=myuser,ou=Staff,ou=Accounts,dc=bfh',
            {
                'userPassword': self.password,
                'uid': ['myuser'],
                'objectClass': ['person', 'organizationalPerson', 'inetOrgPerson', 'posixAccount'],
                'givenName': ['My'],
                'sn': ['User'],
                'mail': ['myuser@example.com'],
                'memberOf': [
                    'cn=IDM.infra.infrastructure.ssh.users,ou=Security,ou=Groups,dc=bfh',
                    'cn=IDM.infra.infrastructure.ntp.admin,ou=Security,ou=Groups,dc=bfh',
                ],
            },
        )
        self.conn.strategy.add_entry(
            'cn=IDM.infra.infrastructure.ssh.users,ou=Security,ou=Groups,dc=bfh',
            {
                'objectClass': ['group'],
            },
        )
        self.conn.strategy.add_entry(
            'cn=IDM.infra.infrastructure.ntp.admin,ou=Security,ou=Groups,dc=bfh',
            {
                'objectClass': ['group'],
                'bfhGroupManager': ['cn=myuser,ou=Staff,ou=Accounts,dc=bfh'],
            },
        )

        # When querying storage list
        self._login('myuser', self.password)
        data = self.getJson("/dashboard/infra.json")

        # Then list of storage is returned.
        self.assertEqual(
            data,
            {
                'data': [
                    {
                        'name': 'ntp',
                        'infra_type': 'infrastructure',
                        'permission': 'admin',
                        'owners': [
                            {'name': 'My User', 'username': 'myuser', 'email': 'myuser@example.com'},
                        ],
                        'group': 'IDM.infra.infrastructure.ntp.admin',
                        'manage_url': 'https://idm.bfh.ch/group/IDM.infra.infrastructure.ntp.admin',
                        'service': 'LNI Infrastructure',
                    },
                    {
                        'name': 'ssh',
                        'infra_type': 'infrastructure',
                        'permission': 'users',
                        'owners': [],
                        'group': 'IDM.infra.infrastructure.ssh.users',
                        'manage_url': None,
                        'service': 'LNI Infrastructure',
                    },
                ]
            },
        )
