File size: 1,770 Bytes
b7a7f32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from typing import Dict, Generator

import pytest
from fastapi.testclient import TestClient
from sqlalchemy.orm import Session

from core.config import settings
from core.db import SessionLocal
from misc.scripts.launch import app
from cruds import crud_user
from schemas import user as user_schemas
import datetime
import requests


headers = {
    "accept": "application/json",
    "Content-Type": "application/json",
}


@pytest.fixture(scope="session")
def db() -> Generator:
    yield SessionLocal()


@pytest.fixture(scope="session")
def super_user_client() -> Generator:
    db = SessionLocal()
    super_user = user_schemas.UserCreate(
        email="test_superadmin@test.local",
        user_type=1,
        password="test",
        full_name="Test Super Admin",
        is_active=True,
        address="Test Land",
        contact_number="9841111111",
        dob=datetime.datetime(2021, 1, 1),
        join_year=2021,
    )
    super_user_obj = crud_user.create(db=db, obj_in=super_user)
    crud_user.verify_user(db=db, db_obj=super_user_obj)

    data = {
        "username": "test_superadmin@test.local",
        "password": "test",
        "remember_me": True,
    }

    super_user_cookies = None
    with TestClient(app) as c:
        req = c.post(
            f"{settings.BACKEND_URL_BASE}{settings.API_V1_STR}/auth/web/",
            json=data,
        )
        super_user_cookies = req.cookies

    assert req.status_code == 200
    assert req.cookies.get("session"), "Cookie not returned!"

    with TestClient(app) as c:
        c.cookies = super_user_cookies
        yield c

    crud_user.remove(db=SessionLocal(), id=super_user_obj.id)


@pytest.fixture(scope="session")
def client() -> Generator:
    with TestClient(app) as c:
        yield c