import csv
import io

from fastapi import status
from fastapi.testclient import TestClient


class TestUsersExportGet:
    PATH = "/v1/users/export"

    def test_returns_csv_with_all_users(
        self,
        test_client: TestClient,
    ) -> None:
        """認証テナントに紐づく全ユーザーをCSVエクスポートできることを確認する。"""
        response = test_client.get(url=self.PATH)

        assert response.status_code == status.HTTP_200_OK
        assert response.headers["content-type"] == "text/csv; charset=utf-8"
        assert 'filename="users.csv"' in response.headers["content-disposition"]

        rows = list(csv.reader(io.StringIO(response.text.removeprefix("\ufeff"))))
        assert rows[0] == ["ID", "名称", "メールアドレス"]
        assert sorted(rows[1:]) == sorted(
            [
                ["admin", "管理ユーザー#アルファ株式会社", ""],
                ["0001", "社員001#アルファ株式会社", "test001@test.com"],
                ["0002", "社員002#アルファ株式会社", "test002@test.com"],
            ],
        )
