def test_team_needs_all_required_fields():
    """Test that teams need to complete profiles before seeing challenges"""
    app = create_ctfd(user_mode="teams")
    with app.app_context():
        # Create a user and team who haven't filled any of their fields
        register_user(app)
        team = gen_team(app.db)
        user = Users.query.filter_by(id=2).first()
        user.team_id = team.id
        team = Teams.query.filter_by(id=1).first()
        team.captain_id = 2
        app.db.session.commit()

        gen_field(
            app.db,
            name="CustomField1",
            type="team",
            required=True,
            public=True,
            editable=True,
        )
        gen_field(
            app.db,
            name="CustomField2",
            type="team",
            required=False,
            public=True,
            editable=True,
        )
        gen_field(
            app.db,
            name="CustomField3",
            type="team",
            required=False,
            public=False,
            editable=True,
        )
        gen_field(
            app.db,
            name="CustomField4",
            type="team",
            required=False,
            public=False,
            editable=False,
        )

        with login_as_user(app) as client:
            r = client.get("/teams/new")
            resp = r.get_data(as_text=True)
            assert "CustomField1" in resp
            assert "CustomField2" in resp
            assert "CustomField3" in resp
            assert "CustomField4" in resp

            # We can't view challenges because we have an incomplete team profile
            r = client.get("/challenges")
            assert r.status_code == 403

            # When we go to our profile we should see all fields
            r = client.get("/team")
            resp = r.get_data(as_text=True)
            assert "CustomField1" in resp
            assert "CustomField2" in resp
            assert "CustomField3" in resp
            assert "CustomField4" in resp

            # Set all non-required fields
            r = client.patch(
                "/api/v1/teams/me",
                json={
                    "fields": [
                        # {"field_id": 1, "value": "CustomFieldEntry1"},
                        {"field_id": 2, "value": "CustomFieldEntry2"},
                        {"field_id": 3, "value": "CustomFieldEntry3"},
                        {"field_id": 4, "value": "CustomFieldEntry4"},
                    ]
                },
            )
            assert r.status_code == 200

            # We can't view challenges because we have an incomplete team profile
            r = client.get("/challenges")
            assert r.status_code == 403

            # Set required fields
            r = client.patch(
                "/api/v1/teams/me",
                json={"fields": [{"field_id": 1, "value": "CustomFieldEntry1"}]},
            )
            assert r.status_code == 200

            # We can view challenges now
            r = client.get("/challenges")
            assert r.status_code == 200

            # Attempts to edit a non-edittable field to field after completing profile
            r = client.patch(
                "/api/v1/teams/me",
                json={"fields": [{"field_id": 4, "value": "CustomFieldEntry4"}]},
            )
            resp = r.get_json()
            assert resp == {
                "success": False,
                "errors": {"fields": ["Field 'CustomField4' cannot be editted"]},
            }

            # I can edit edittable fields
            r = client.patch(
                "/api/v1/teams/me",
                json={
                    "fields": [
                        {"field_id": 1, "value": "CustomFieldEntry1"},
                        {"field_id": 2, "value": "CustomFieldEntry2"},
                        {"field_id": 3, "value": "CustomFieldEntry3"},
                    ]
                },
            )
            assert r.status_code == 200

            # I should see the correct fields in the private team profile
            r = client.get("/team")
            resp = r.get_data(as_text=True)
            assert "CustomField1" in resp
            assert "CustomField2" in resp
            assert (
                "CustomField3" in resp
            )  # This is here because /team contains team settings
            assert "CustomField4" not in resp

            # I should see the correct fields in the public team profile
            r = client.get("/teams/1")
            resp = r.get_data(as_text=True)
            assert "CustomField1" in resp
            assert "CustomField2" in resp
            assert "CustomField3" not in resp
            assert "CustomField4" not in resp
    destroy_ctfd(app)
Test that teams need to complete profiles before seeing challenges
tests/teams/test_fields.py