# pylint: disable=invalid-name,missing-docstring,no-self-use

import pytest

from ensign import BinaryFlag


@pytest.mark.component
@pytest.mark.usefixtures("db")
class TestFlagsAPI:
    def test_get(self, api):
        BinaryFlag.create(
            "flag0",
            label="Fake flag",
            description="Flag for testing purposes",
            tags="test,fake",
        )
        response = api.get("/flags/flag0")

        assert response.status_code == 200
        assert response.json["name"] == "flag0"

    def test_collection_get(self, api):
        names = ["flag0", "flag1", "flag42"]
        for name in names:
            BinaryFlag.create(name)
        response = api.get("/flags")

        assert response.status_code == 200
        assert len(response.json) == len(names)
        for item in response.json:
            assert item["name"] in names

    def test_post(self, api):
        payload = {
            "name": "test_flag",
            "label": "Test flag",
            "description": "Flag for testing purposes",
            "tags": "test,flag,fake",
        }
        response = api.post_json("/flags", payload)
        flag = BinaryFlag("test_flag")

        assert response.status_code == 201
        assert flag.info == payload

    def test_patch(self, api):
        payload = {
            "value": True,
        }
        flag = BinaryFlag.create("flag0")
        response = api.patch_json("/flags/flag0", payload)

        assert response.status_code == 204
        assert flag

    def test_flag_flow(self, api):
        response = api.post_json("/flags", {"name": "flag_flow"})
        assert response.status_code == 201

        response = api.get("/flags")
        assert response.status_code == 200
        assert len(response.json) == 1

        response = api.patch_json("/flags/flag_flow", {"value": True})
        assert response.status_code == 204

        response = api.get("/flags/flag_flow")
        assert response.status_code == 200
        assert response.json["value"]


@pytest.mark.component
@pytest.mark.usefixtures("db")
class TestFlagsAPIErrors:
    def test_get_404(self, api):
        api.get("/flags/flag314", status=404)

    def test_post_400_empty(self, api):
        api.post_json("/flags", {}, status=400)

    def test_post_400_noname(self, api):
        api.post_json("/flags", {
            "label": "Unnamed flag",
            "description": "No name flag",
        }, status=400)

    def test_patch_400_novalue(self, api):
        BinaryFlag.create("flag0")
        api.patch_json("/flags/flag0", {}, status=400)

    def test_patch_400_extrafields(self, api):
        BinaryFlag.create("flag0")
        api.patch_json("/flags/flag0", {
            "value": True,
            "label": "Label",
        }, status=400)

    def test_patch_404(self, api):
        api.patch_json("/flags/flag0", {
            "value": True,
        }, status=404)

@@ -422,53 +422,22 @@
 ag0%22
-)%0A%0A        assert response.status_code == 
+, status=
 200
+)%0A
 %0A   
@@ -669,53 +669,22 @@
 ags%22
-)%0A%0A        assert response.status_code == 
+, status=
 200
+)%0A
 %0A   
@@ -1076,16 +1076,28 @@
  payload
+, status=201
 )%0A      
@@ -1134,51 +1134,8 @@
 %22)%0A%0A
-        assert response.status_code == 201%0A
     
@@ -1358,53 +1358,22 @@
 load
-)%0A%0A        assert response.status_code == 
+, status=
 204
+)%0A
 %0A   
@@ -1458,25 +1458,50 @@
 st_json(
-%22/flags%22,
+%0A            %22/flags%22,%0A           
  %7B%22name%22
@@ -1514,17 +1514,17 @@
 g_flow%22%7D
-)
+,
 %0A       
@@ -1528,42 +1528,33 @@
     
-assert response.status_code == 201
+    status=201,%0A        )
 %0A%0A  
@@ -1590,52 +1590,21 @@
 ags%22
-)%0A        assert response.status_code == 
+, status=
 200
+)
 %0A   
@@ -1666,32 +1666,45 @@
  api.patch_json(
+%0A            
 %22/flags/flag_flo
@@ -1706,16 +1706,28 @@
 g_flow%22,
+%0A           
  %7B%22value
@@ -1734,17 +1734,17 @@
 %22: True%7D
-)
+,
 %0A       
@@ -1748,42 +1748,33 @@
     
-assert response.status_code == 204
+    status=204,%0A        )
 %0A%0A  
@@ -1820,52 +1820,21 @@
 low%22
-)%0A        assert response.status_code == 
+, status=
 200
+)
 %0A   

Use WebTest's facilities to check status codes.