def test_adds_prohibited_project_name_with_deletes(self, db_request):
        db_request.user = UserFactory.create()
        db_request.POST["project"] = "foo"
        db_request.POST["confirm"] = "foo"
        db_request.POST["comment"] = "This is a comment"
        db_request.session = pretend.stub(
            flash=pretend.call_recorder(lambda *a, **kw: None)
        )
        db_request.route_path = lambda a: "/admin/prohibited_project_names/"
        db_request.remote_addr = "192.168.1.1"

        project = ProjectFactory.create(name="foo")
        release = ReleaseFactory.create(project=project)
        FileFactory.create(release=release, filename="who cares")
        RoleFactory.create(project=project, user=db_request.user)

        views.add_prohibited_project_names(db_request)

        assert db_request.session.flash.calls == [
            pretend.call("Deleted the project 'foo'", queue="success"),
            pretend.call("Prohibited Project Name 'foo'", queue="success"),
        ]

        prohibited_project_name = (
            db_request.db.query(ProhibitedProjectName)
            .filter(ProhibitedProjectName.name == "foo")
            .one()
        )

        assert prohibited_project_name.name == "foo"
        assert prohibited_project_name.prohibited_by == db_request.user
        assert prohibited_project_name.comment == "This is a comment"

        assert not (db_request.db.query(Project).filter(Project.name == "foo").count())
    def test_adds_prohibited_project_name(self, db_request):
        db_request.user = UserFactory.create()
        db_request.POST["project"] = "foo"
        db_request.POST["confirm"] = "foo"
        db_request.POST["comment"] = "This is a comment"
        db_request.session = pretend.stub(
            flash=pretend.call_recorder(lambda *a, **kw: None)
        )
        db_request.route_path = lambda a: "/admin/prohibited_project_names/"

        views.add_prohibited_project_names(db_request)

        assert db_request.session.flash.calls == [
            pretend.call("Prohibited Project Name 'foo'", queue="success")
        ]

        prohibited_project_name = (
            db_request.db.query(ProhibitedProjectName)
            .filter(ProhibitedProjectName.name == "foo")
            .one()
        )

        assert prohibited_project_name.name == "foo"
        assert prohibited_project_name.prohibited_by == db_request.user
        assert prohibited_project_name.comment == "This is a comment"
    def test_wrong_confirm(self):
        request = pretend.stub(
            POST={"project": "foo", "confirm": "bar"},
            session=pretend.stub(flash=pretend.call_recorder(lambda *a, **kw: None)),
            current_route_path=lambda: "/foo/bar/",
        )

        result = views.add_prohibited_project_names(request)

        assert request.session.flash.calls == [
            pretend.call("'bar' is not the same as 'foo'", queue="error")
        ]
        assert result.status_code == 303
        assert result.headers["Location"] == "/foo/bar/"
    def test_already_existing_prohibited_project_names(self, db_request):
        prohibited_project_name = ProhibitedProjectFactory.create()

        db_request.db.expire_all()
        db_request.user = UserFactory.create()
        db_request.POST["project"] = prohibited_project_name.name
        db_request.POST["confirm"] = prohibited_project_name.name
        db_request.POST["comment"] = "This is a comment"
        db_request.session = pretend.stub(
            flash=pretend.call_recorder(lambda *a, **kw: None))
        db_request.route_path = lambda a: "/admin/prohibited_project_names/"

        result = views.add_prohibited_project_names(db_request)

        assert db_request.session.flash.calls == [
            pretend.call(
                f"{prohibited_project_name.name!r} has already been prohibited.",
                queue="error",
            )
        ]
        assert result.status_code == 303
        assert result.headers["Location"] == "/admin/prohibited_project_names/"
    def test_no_project(self):
        request = pretend.stub(POST={})

        with pytest.raises(HTTPBadRequest):
            views.add_prohibited_project_names(request)