-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_services_configuration.py
162 lines (115 loc) · 4.96 KB
/
test_services_configuration.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
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
#!/usr/bin/python3
import os
import requests
from genezio import genezio_deploy, genezio_delete
def test_genezio_services():
print("Starting services configuration test...")
token = os.environ.get('GENEZIO_TOKEN')
os.chdir("./projects/services-configuration/")
deploy_result = genezio_deploy(False, with_config="./genezio-database.yaml")
assert deploy_result.return_code == 0, "genezio deploy returned non-zero exit code"
assert deploy_result.project_url != "", "genezio deploy returned empty project url"
is_database_result = get_database("it-gnz-database", token)
assert is_database_result != None, "Database not created"
print("Database test passed.")
deploy_result = genezio_deploy(False, with_config="./genezio-email.yaml")
assert deploy_result.return_code == 0, "genezio deploy returned non-zero exit code"
assert deploy_result.project_url != "", "genezio deploy returned empty project url"
is_email_result = is_email(token)
assert is_email_result, "Email service not enabled"
print("Email test passed.")
deploy_result = genezio_deploy(False, with_config="./genezio-authentication.yaml")
assert deploy_result.return_code == 0, "genezio deploy returned non-zero exit code"
assert deploy_result.project_url != "", "genezio deploy returned empty project url"
is_authentication_result = is_auth("services-configuration", token)
assert is_authentication_result, "Authentication not enabled"
auth_providers_result = check_auth_providers("services-configuration", token)
assert auth_providers_result, "Authentication providers not enabled"
print("Authentication test passed.")
print("Cleanup resources...")
genezio_delete(deploy_result.project_id)
print("Test passed!")
def get_database(name: str, token: str):
backend_endpoint = os.environ.get('GENEZIO_BACKEND_ENDPOINT')
url = f"{backend_endpoint}/databases"
payload = ""
headers = {
'Accept-Version': 'cli/2.4.2',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token,
}
response = requests.request("GET", url, headers=headers, data=payload)
data = response.json()
# Iterate over the databases to see if any match the provided name
for db in data.get('databases', []):
if db.get('name') == name:
return db.get('id')
return None
def is_email(token: str) -> bool:
environment = get_backend_environment("services-configuration", token)
for env in environment:
if env.get('name') == 'EMAIL_SERVICE_TOKEN':
return True
return False
def is_auth(name: str, token: str) -> bool:
_, envId = get_project_name(name, token)
backend_endpoint = os.environ.get('GENEZIO_BACKEND_ENDPOINT')
url = f"{backend_endpoint}/core/auth/{envId}"
payload = ""
headers = {
'Accept-Version': 'cli/2.4.2',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token,
}
response = requests.request("GET", url, headers=headers, data=payload)
data = response.json()
return data.get('enabled')
def check_auth_providers(name: str, token: str) -> bool:
_, envId = get_project_name(name, token)
backend_endpoint = os.environ.get('GENEZIO_BACKEND_ENDPOINT')
url = f"{backend_endpoint}/core/auth/providers/{envId}"
payload = ""
headers = {
'Accept-Version': 'cli/2.4.2',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token,
}
response = requests.request("GET", url, headers=headers, data=payload)
data = response.json()
for provider in data.get('authProviders'):
if provider.get('enabled') == False:
return False
return True
def get_backend_environment(name:str, token: str) -> str:
projectId, envId = get_project_name(name, token)
backend_endpoint = os.environ.get('GENEZIO_BACKEND_ENDPOINT')
url = f"{backend_endpoint}/projects/{projectId}/{envId}/environment-variables"
payload = ""
headers = {
'Accept-Version': 'cli/2.4.2',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token,
}
response = requests.request("GET", url, headers=headers, data=payload)
data = response.json()
return data.get('environmentVariables')
def get_project_name(name: str, token: str):
backend_endpoint = os.environ.get('GENEZIO_BACKEND_ENDPOINT')
url = f"{backend_endpoint}/projects/name/{name}"
payload = ""
headers = {
'Accept-Version': 'cli/2.4.2',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token,
}
response = requests.request("GET", url, headers=headers, data=payload)
data = response.json()
projectId = data.get('project').get('id')
for env in data.get('project').get('projectEnvs'):
if env.get('name') == 'prod':
envId = env.get('id')
break
return projectId, envId
# Test order matters because the commands are having side effects.
if __name__ == '__main__':
test_genezio_services()