# Copyright (C) 2010-2024 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License, version 3
# (only), as published by the Free Software Foundation.
#
# 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 Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# This program is dual-licensed. If you wish to learn more about the
# RhodeCode Enterprise Edition, including its added features, Support services,
# and proprietary license terms, please see https://rhodecode.com/licenses/

import pytest

from rhodecode.api.tests.utils import (
    build_data, api_call)


@pytest.mark.usefixtures("app")
class TestServiceApi:

    def test_service_api_with_wrong_secret(self):
        id, payload = build_data("wrong_api_key", 'service_get_repo_name_by_id')
        response = api_call(self.app, payload)

        assert 'Invalid API KEY' == response.json['error']

    def test_service_api_with_legit_secret(self):
        id, payload = build_data(self.app.app.config.get_settings()['app.service_api.token'],
                                 'service_get_repo_name_by_id', repo_id='1')
        response = api_call(self.app, payload)
        assert not response.json['error']

    def test_service_api_not_a_part_of_public_api_suggestions(self):
        id, payload = build_data("secret", 'some_random_guess_method')
        response = api_call(self.app, payload)
        assert 'service_' not in response.json['error']

    def test_service_get_data_for_ssh_wrapper_output(self):
        id, payload = build_data(
            self.app.app.config.get_settings()['app.service_api.token'],
            'service_get_data_for_ssh_wrapper',
            user_id=1,
            repo_name='vcs_test_git')
        response = api_call(self.app, payload)

        assert ['branch_permissions', 'repo_permissions', 'repos_path', 'user_id', 'username']\
               == list(response.json['result'].keys())