# 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.model.db import Repository
from rhodecode.tests.routes import route_path
from rhodecode.tests import assert_session_flash


@pytest.mark.usefixtures("autologin_user", "app")
class TestBranchesController(object):
    def test_index(self, backend):
        response = self.app.get(route_path("branches_home", repo_name=backend.repo_name))

        repo = Repository.get_by_repo_name(backend.repo_name)

        for commit_id, obj_name in repo.scm_instance().branches.items():
            assert commit_id in response
            assert obj_name in response

    def test_landing_branch_delete(self, backend, csrf_token):
        if backend.alias == "svn":
            pytest.skip("Not supported yet")
        branch_related_data_per_backend = {
            "git": {"name": "master"},
            "hg": {"name": "default"},
        }
        response = self.app.post(
            route_path(
                "branch_remove",
                repo_name=backend.repo_name,
                branch_name=branch_related_data_per_backend[backend.alias]["name"],
            ),
            params={"csrf_token": csrf_token},
            status=302,
        )
        assert_session_flash(
            response,
            f"This branch {branch_related_data_per_backend[backend.alias]['name']} cannot be removed as it's currently set as landing branch",
        )

    def test_delete_branch_by_repo_owner(self, backend, csrf_token):
        if backend.alias in ("svn", "hg"):
            pytest.skip("Skipping for hg and svn")
        branch_to_be_removed = "remove_me"
        repo = Repository.get_by_repo_name(backend.repo_name)
        repo.scm_instance()._create_branch(branch_to_be_removed, repo.scm_instance().commit_ids[1])
        response = self.app.post(
            route_path("branch_remove", repo_name=backend.repo_name, branch_name=branch_to_be_removed),
            params={"csrf_token": csrf_token},
            status=302,
        )
        assert_session_flash(response, f"Branch {branch_to_be_removed} has been successfully deleted")

    def test_delete_branch_by_not_repo_owner(self, backend, csrf_token):
        username = "test_regular"
        pwd = "test12"
        branch_related_data_per_backend = {
            "git": {"name": "master", "action": "deleted"},
            "hg": {"name": "stable", "action": "closed"},
        }
        if backend.alias == "svn":
            pytest.skip("Not supported yet")
        self.app.post(route_path("login"), {"username": username, "password": pwd})
        selected_branch = branch_related_data_per_backend[backend.alias]["name"]
        response = self.app.post(
            route_path("branch_remove", repo_name=backend.repo_name, branch_name=selected_branch),
            params={"csrf_token": csrf_token, "username": username, "password": pwd},
            status=404,
        )
        assert response.status_code == 404
