# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest

from pretend import call, call_recorder, raiser, stub
from pyramid.httpexceptions import HTTPSeeOther
from sqlalchemy.orm import joinedload

from warehouse.packaging.models import (
    Dependency,
    File,
    JournalEntry,
    Project,
    Release,
    Role,
)
from warehouse.utils.project import (
    confirm_project,
    destroy_docs,
    remove_documentation,
    remove_project,
)

from ...common.db.accounts import UserFactory
from ...common.db.packaging import (
    DependencyFactory,
    FileFactory,
    ProjectFactory,
    ReleaseFactory,
    RoleFactory,
)


def test_confirm():
    project = stub(normalized_name="foobar")
    request = stub(
        POST={"confirm_project_name": "foobar"},
        route_path=call_recorder(lambda *a, **kw: stub()),
        session=stub(flash=call_recorder(lambda *a, **kw: stub())),
    )

    confirm_project(project, request, fail_route="fail_route")

    assert request.route_path.calls == []
    assert request.session.flash.calls == []


def test_confirm_no_input():
    project = stub(normalized_name="foobar")
    request = stub(
        POST={"confirm_project_name": ""},
        route_path=call_recorder(lambda *a, **kw: "/the-redirect"),
        session=stub(flash=call_recorder(lambda *a, **kw: stub())),
    )

    with pytest.raises(HTTPSeeOther) as err:
        confirm_project(project, request, fail_route="fail_route")
    assert err.value.location == "/the-redirect"

    assert request.route_path.calls == [call("fail_route", project_name="foobar")]
    assert request.session.flash.calls == [call("Confirm the request", queue="error")]


def test_confirm_incorrect_input():
    project = stub(normalized_name="foobar")
    request = stub(
        POST={"confirm_project_name": "bizbaz"},
        route_path=call_recorder(lambda *a, **kw: "/the-redirect"),
        session=stub(flash=call_recorder(lambda *a, **kw: stub())),
    )

    with pytest.raises(HTTPSeeOther) as err:
        confirm_project(project, request, fail_route="fail_route")
    assert err.value.location == "/the-redirect"

    assert request.route_path.calls == [call("fail_route", project_name="foobar")]
    assert request.session.flash.calls == [
        call(
            "Could not delete project - 'bizbaz' is not the same as 'foobar'",
            queue="error",
        )
    ]


@pytest.mark.parametrize("flash", [True, False])
def test_remove_project(db_request, flash):