-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_main.py
132 lines (112 loc) · 4.25 KB
/
test_main.py
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
from fastapi.testclient import TestClient
import os
from main import app, create_access_token
client = TestClient(app)
async def override_login(q: str | None = None):
return {
"access_token": create_access_token(
data={"sub": "test_user"}, expires_delta=None
),
"token_type": "bearer",
}
# app.dependency_overrides[token] = override_login
def get_bearer_token():
return {
"access_token": create_access_token(
data={"sub": "test_user"}, expires_delta=None
),
"token_type": "bearer",
}
return "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJqb2huZG9lIiwiZXhwIjoxNjkyMjE0NzIxfQ.uM4XR2Yqndbzwb6KM5fu5_TXmJ-C8SJ3mjuWDd7wXE"
return os.environ["BEARER_TOKEN"]
def test_file_exists():
os.environ["VAULT_PATH"] = "./test_vault"
response = client.get(
"/api/v1/exists?path=file1.md",
headers={"Authorization": f"Bearer {get_bearer_token()}"},
)
assert response.status_code == 200
assert response.json() == {"file exists": True}
response = client.get(
"/api/v1/exists?path=file3.md",
headers={"Authorization": f"Bearer {get_bearer_token()}"},
)
assert response.status_code == 200
assert response.json() == {"file exists": False}
response = client.get(
"/api/v1/exists", headers={"Authorization": f"Bearer {get_bearer_token()}"}
)
assert response.status_code == 400
assert response.json() == {"detail": "Path not provided"}
def test_append_to_file():
initial_file_content = ""
fake_data = {"path": "file1.md", "text": "Hello World"}
os.environ["VAULT_PATH"] = "./test_vault"
with open(
os.path.join(os.environ["VAULT_PATH"], fake_data["path"]), "r", encoding="UTF-8"
) as f:
initial_file_content = f.read()
response = client.post(
"/api/v1/append",
json=fake_data,
headers={"Authorization": f"Bearer {get_bearer_token()}"},
)
with open(
os.path.join(os.environ["VAULT_PATH"], fake_data["path"]), "r", encoding="UTF-8"
) as f:
assert f.read() == initial_file_content + "\n" + fake_data["text"]
assert response.status_code == 200
assert response.json() == {
"action": f'added {fake_data["text"]} to {fake_data["path"]}'
}
with open(
os.path.join(os.environ["VAULT_PATH"], fake_data["path"]), "w", encoding="UTF-8"
) as f:
f.write(initial_file_content)
# TODO: maybe add test cases where the file does not exist or the path is not provided
def test_file_content():
os.environ["VAULT_PATH"] = "./test_vault"
response = client.get(
"/api/v1/content?path=file1.md",
headers={"Authorization": f"Bearer {get_bearer_token()}"},
)
assert response.status_code == 200
assert response.json() == {
"content": "# File 1\n---\ntag: tag1, tag2\nkeywords: keyword1, keyword2\n---"
}
response = client.get(
"/api/v1/content?path=file3.md",
headers={"Authorization": f"Bearer {get_bearer_token()}"},
)
assert response.status_code == 400
assert response.json() == {"detail": "File does not exist"}
def test_file_metadata():
os.environ["VAULT_PATH"] = "./test_vault"
response = client.get(
"/api/v1/metadata?path=file1.md",
headers={"Authorization": f"Bearer {get_bearer_token()}"},
)
assert response.status_code == 200
assert response.json() == {
"metadata": {"tag": "tag1, tag2", "keywords": "keyword1, keyword2"}
}
response = client.get(
"/api/v1/metadata?path=file1_metadata_lower_in_the_page.md",
headers={"Authorization": f"Bearer {get_bearer_token()}"},
)
assert response.status_code == 200
assert response.json() == {
"metadata": {"tag": "tag1, tag2", "keywords": "keyword1, keyword2"}
}
response = client.get(
"/api/v1/metadata?path=file2.md",
headers={"Authorization": f"Bearer {get_bearer_token()}"},
)
assert response.status_code == 200
assert response.json() == {"metadata": {}}
response = client.get(
"/api/v1/metadata?path=file3.md",
headers={"Authorization": f"Bearer {get_bearer_token()}"},
)
assert response.status_code == 400
assert response.json() == {"detail": "File does not exist"}