-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
190 lines (175 loc) · 5.4 KB
/
conftest.py
File metadata and controls
190 lines (175 loc) · 5.4 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
"""Create test user.
Copyright (c) 2024 MultiFactor
License: https://github.com/MultiDirectoryLab/MultiDirectory/blob/main/LICENSE
"""
import pytest_asyncio
from fastapi import FastAPI
from httpx import ASGITransport, AsyncClient
from sqlalchemy.ext.asyncio import AsyncSession
from entities import CatalogueSetting
from ldap_protocol.dns import (
DNS_MANAGER_IP_ADDRESS_NAME,
DNS_MANAGER_STATE_NAME,
DNS_MANAGER_ZONE_NAME,
DNSManagerState,
)
from ldap_protocol.ldap_codes import LDAPCodes
from ldap_protocol.ldap_requests.modify import Operation
@pytest_asyncio.fixture(scope="function")
async def adding_test_user(
app: FastAPI,
http_client: AsyncClient,
_force_override_tls: None,
) -> None:
"""Test add user like keycloak."""
test_user_dn = "cn=test,dc=md,dc=test"
user_password = '"\x00P\x00@\x00s\x00s\x00w\x000\x00r\x00d\x00"\x00' # noqa
response = await http_client.post(
"/entry/add",
json={
"entry": test_user_dn,
"password": None,
"attributes": [
{
"type": "name",
"vals": ["test"],
},
{
"type": "cn",
"vals": ["test"],
},
{
"type": "testing_attr",
"vals": ["test"],
},
{
"type": "sAMAccountName",
"vals": ["test"],
},
{
"type": "objectClass",
"vals": [
"top",
"user",
"person",
"organizationalPerson",
"posixAccount",
"shadowAccount",
"inetOrgPerson",
],
},
],
},
)
data = response.json()
assert data["resultCode"] == LDAPCodes.SUCCESS
response = await http_client.patch(
"/entry/update",
json={
"object": test_user_dn,
"changes": [
{
"operation": Operation.ADD,
"modification": {
"type": "mail",
"vals": ["new_user@md.test"],
},
},
{
"operation": Operation.ADD,
"modification": {
"type": "userPrincipalName",
"vals": ["new_user@md.test"],
},
},
{
"operation": Operation.ADD,
"modification": {
"type": "displayName",
"vals": ["Test User"],
},
},
{
"operation": Operation.ADD,
"modification": {
"type": "unicodePwd",
"vals": [user_password],
},
},
{
"operation": Operation.ADD,
"modification": {
"type": "memberOf",
"vals": ["cn=domain admins,cn=Groups,dc=md,dc=test"],
},
},
{
"operation": Operation.ADD,
"modification": {
"type": "userAccountControl",
"vals": ["0"],
},
},
],
},
)
data = response.json()
assert data["resultCode"] == LDAPCodes.SUCCESS
async with AsyncClient(
transport=ASGITransport(app=app, root_path="/api"),
timeout=3,
base_url="http://test",
) as client:
auth = await client.post(
"auth/",
data={
"username": "new_user@md.test",
"password": "P@ssw0rd",
},
)
assert auth.cookies.get("id")
@pytest_asyncio.fixture(scope="function")
async def adding_test_computer(
http_client: AsyncClient,
) -> None:
"""Test api correct (name) add."""
response = await http_client.post(
"/entry/add",
json={
"entry": "cn=mycomputer,dc=md,dc=test",
"password": None,
"attributes": [
{"type": "name", "vals": ["mycomputer name"]},
{"type": "cn", "vals": ["mycomputer"]},
{"type": "objectClass", "vals": ["computer", "top"]},
],
},
)
data = response.json()
assert isinstance(data, dict)
assert data.get("resultCode") == LDAPCodes.SUCCESS
@pytest_asyncio.fixture(scope="function")
async def add_dns_settings(
session: AsyncSession,
) -> None:
"""Add DNS manager settings to DB."""
dns_ip_address = "127.0.0.1"
domain = "example.com"
dns_state = DNSManagerState.HOSTED
session.add_all(
[
CatalogueSetting(
name=DNS_MANAGER_IP_ADDRESS_NAME,
value=dns_ip_address,
),
CatalogueSetting(
name=DNS_MANAGER_ZONE_NAME,
value=domain,
),
CatalogueSetting(
name=DNS_MANAGER_STATE_NAME,
value=dns_state,
),
],
)
await session.commit()