1
1
import os
2
2
import net.http
3
3
import time
4
+ import json
5
+ import api
4
6
5
- const myfolder = os.dir (os.executable ())
7
+ const gitly_url = 'http://127.0.0.1:8080'
8
+
9
+ const default_branch = 'main'
10
+
11
+ const test_username = 'bob'
12
+
13
+ const test_github_repo_url = 'https://github.com/vlang/ui'
14
+
15
+ const test_github_repo_primary_branch = 'master'
16
+
17
+ fn main () {
18
+ before ()?
19
+
20
+ test_index_page ()
21
+
22
+ ilog ('Register the first user `$test_username `' )
23
+ mut register_headers , token := register_user (test_username, '1234zxcv' , 'bob@example.com' ) or {
24
+ exit_with_message (err.str ())
25
+ }
26
+
27
+ ilog ('Check all cookies that must be present' )
28
+ assert register_headers.contains (.set_cookie)
29
+
30
+ ilog ('Ensure the login token is present after registration' )
31
+ has_token := token != ''
32
+ assert has_token
33
+
34
+ test_user_page (test_username)
35
+ test_login_with_token (test_username, token)
36
+ test_static_served ()
37
+
38
+ test_create_repo (token, 'test1' , '' )
39
+ assert get_repo_commit_count (token, test_username, 'test1' , default_branch) == 0
40
+ assert get_repo_issue_count (token, test_username, 'test1' ) == 0
41
+ assert get_repo_branch_count (token, test_username, 'test1' ) == 0
42
+
43
+ test_create_repo (token, 'test2' , test_github_repo_url)
44
+ assert get_repo_commit_count (token, test_username, 'test2' , test_github_repo_primary_branch) > 0
45
+ assert get_repo_issue_count (token, test_username, 'test2' ) == 0
46
+ assert get_repo_branch_count (token, test_username, 'test2' ) > 0
47
+
48
+ after ()?
49
+ }
50
+
51
+ fn before () ? {
52
+ cd_executable_dir ()?
53
+
54
+ ilog ('Make sure gitly is not running' )
55
+ kill_gitly_processes ()
56
+
57
+ remove_database_if_exists ()?
58
+ remove_repos_dir_if_exists ()?
59
+ compile_gitly ()
60
+
61
+ ilog ('Start gitly in the background, then wait till gitly starts and is responding to requests' )
62
+ go run_gitly ()
63
+
64
+ wait_gitly ()
65
+ }
66
+
67
+ fn after () ? {
68
+ remove_database_if_exists ()?
69
+ remove_repos_dir_if_exists ()?
70
+
71
+ ilog ('Ensure gitly is stopped' )
72
+ kill_gitly_processes ()
73
+ }
74
+
75
+ fn run_gitly () {
76
+ gitly_process := os.execute ('./gitly &' )
77
+ if gitly_process.exit_code != 0 {
78
+ exit_with_message (gitly_process.str ())
79
+ }
80
+ }
6
81
7
82
[noreturn]
8
83
fn exit_with_message (message string ) {
@@ -14,33 +89,56 @@ fn ilog(message string) {
14
89
println ('$time.now ().format_ss_milli() | $message ' )
15
90
}
16
91
17
- fn main () {
92
+ fn cd_executable_dir () ? {
93
+ executable_dir := os.dir (os.executable ())
18
94
// Ensure that we are always running in the gitly folder, no matter what is the starting one:
19
- os.chdir (os.dir (myfolder))?
95
+ os.chdir (os.dir (executable_dir))?
96
+
20
97
ilog ('Testing first gitly run.' )
98
+ }
21
99
22
- ilog ( 'Make sure gitly is not running' )
100
+ fn kill_gitly_processes () {
23
101
os.execute ('pkill -9 gitly' )
102
+ }
24
103
104
+ fn remove_database_if_exists () ? {
25
105
ilog ('Remove old gitly DB' )
106
+
26
107
if os.exists ('gitly.sqlite' ) {
27
108
os.rm ('gitly.sqlite' )?
28
109
}
110
+ }
111
+
112
+ fn remove_repos_dir_if_exists () ? {
113
+ ilog ('Remove repos directory' )
114
+
115
+ if os.exists ('repos' ) {
116
+ os.rmdir_all ('repos' )?
117
+ }
118
+ }
29
119
120
+ fn compile_gitly () {
30
121
ilog ('Compile gitly' )
122
+
31
123
os.execute ('v .' )
124
+ }
32
125
33
- ilog ('Start gitly in the background, then wait till gitly starts and is responding to requests' )
34
- go run_gitly ()
126
+ fn wait_gitly () {
35
127
for waiting_cycles := 0 ; waiting_cycles < 50 ; waiting_cycles++ {
36
- ilog (' wait : $waiting_cycles ' )
128
+ ilog ('\t wait : $waiting_cycles ' )
37
129
time.sleep (100 * time.millisecond)
38
- http.get ('http://127.0.0.1:8080' ) or { continue }
130
+ http.get (prepare_url ( '' ) ) or { continue }
39
131
break
40
132
}
133
+ }
41
134
135
+ fn prepare_url (path string ) string {
136
+ return '$gitly_url /$path '
137
+ }
138
+
139
+ fn test_index_page () {
42
140
ilog ("Ensure gitly's main page is up" )
43
- index_page_result := http.get ('http://127.0.0.1:8080' ) or { exit_with_message (err.str ()) }
141
+ index_page_result := http.get (prepare_url ( '' ) ) or { exit_with_message (err.str ()) }
44
142
assert index_page_result.body.contains ('<html>' )
45
143
assert index_page_result.body.contains ('</html>' )
46
144
@@ -51,56 +149,116 @@ fn main() {
51
149
52
150
// Make sure no one's logged in
53
151
assert index_page_result.body.contains ("<a href='/login' class='login-button'>Log in</a>" )
152
+ }
54
153
55
- ilog ('Register the first user `bob`' )
56
- mut register_result := http.post ('http://127.0.0.1:8080/register' , 'username=bob&password=1234zxcv&email=bob@example.com&no_redirect=1' ) or {
57
- exit_with_message (err.str ())
154
+ // returns headers and token
155
+ fn register_user (username string , password string , email string ) ? (http.Header, string ) {
156
+ response := http.post (prepare_url ('register' ), 'username=$username &password=$password &email=$email &no_redirect=1' ) or {
157
+ return err
58
158
}
59
- ilog ('Check all cookies that must be present' )
60
- assert register_result.header.contains (.set_cookie)
61
159
62
- ilog ('Ensure the login token is present after registration' )
63
- mut has_token := false
64
160
mut token := ''
65
- for val in register_result .header.values (.set_cookie) {
161
+ for val in response .header.values (.set_cookie) {
66
162
token = val.find_between ('token=' , ';' )
67
- has_token = token != ''
68
163
}
69
- assert has_token
70
164
71
- ilog ('Testing the new user /bob page is up after registration' )
72
- user_page_result := http.get ('http://127.0.0.1:8080/bob' ) or { exit_with_message (err.str ()) }
73
- assert user_page_result.body.contains ('<h3>bob</h3>' )
165
+ return response.header, token
166
+ }
167
+
168
+ fn test_static_served () {
169
+ ilog ('Ensure that static css is served' )
170
+ css := http.get (prepare_url ('css/gitly.css' )) or { exit_with_message (err.str ()) }
171
+
172
+ assert css.status_code != 404
173
+ assert css.body.contains ('body' )
174
+ assert css.body.contains ('html' )
175
+ }
176
+
177
+ fn test_user_page (username string ) {
178
+ ilog ('Testing the new user /$username page is up after registration' )
179
+ user_page_result := http.get (prepare_url (username)) or { exit_with_message (err.str ()) }
180
+
181
+ assert user_page_result.body.contains ('<h3>$username </h3>' )
182
+ }
183
+
184
+ fn test_login_with_token (username string , token string ) {
185
+ ilog ('Try to login in with `$username ` user token' )
74
186
75
- ilog ('Try to login in with `bob` user token' )
76
187
login_result := http.fetch (
77
188
method: .get
78
189
cookies: {
79
190
'token' : token
80
191
}
81
- url: 'http://127.0.0.1:8080/bob'
192
+ url: prepare_url (username)
82
193
) or { exit_with_message (err.str ()) }
83
194
84
- ilog ('Ensure that after login, there is a signed in as `bob` message' )
195
+ ilog ('Ensure that after login, there is a signed in as `$username ` message' )
196
+
85
197
assert login_result.body.contains ('<span>Signed in as</span>' )
86
- assert login_result.body.contains ("<a href='/bob'>bob</a>" )
198
+ assert login_result.body.contains ("<a href='/$username '>$username </a>" )
199
+ }
87
200
88
- ilog ('Ensure that static css is served' )
89
- css := http.get ('http://127.0.0.1:8080/css/gitly.css' ) or { exit_with_message (err.str ()) }
201
+ fn test_create_repo (token string , name string , clone_url string ) {
202
+ description := 'test description'
203
+ repo_visibility := 'public'
90
204
91
- println (css)
205
+ response := http.fetch (
206
+ method: .post
207
+ cookies: {
208
+ 'token' : token
209
+ }
210
+ url: prepare_url ('new' )
211
+ data: 'name=$name &description=$description &clone_url=$clone_url &repo_visibility=$repo_visibility &no_redirect=1'
212
+ ) or { exit_with_message (err.str ()) }
92
213
93
- assert css .status_code != 404
94
- assert css .body. contains ( 'body' )
95
- assert css.body. contains ( 'html' )
214
+ assert response .status_code == 200
215
+ assert response .body == 'ok'
216
+ }
96
217
97
- ilog ('Ensure gitly is stopped' )
98
- os.execute ('pkill -9 gitly' )
218
+ fn get_repo_commit_count (token string , username string , repo_name string , branch_name string ) int {
219
+ response := http.fetch (
220
+ method: .get
221
+ cookies: {
222
+ 'token' : token
223
+ }
224
+ url: prepare_url ('api/v1/$username /$repo_name /$branch_name /commits/count' )
225
+ ) or { exit_with_message (err.str ()) }
226
+
227
+ response_json := json.decode (api.ApiCommitCount, response.body) or {
228
+ exit_with_message (err.str ())
229
+ }
230
+
231
+ return response_json.result
99
232
}
100
233
101
- fn run_gitly () {
102
- gitly_process := os.execute ('./gitly &' )
103
- if gitly_process.exit_code != 0 {
104
- exit_with_message (gitly_process.str ())
234
+ fn get_repo_issue_count (token string , username string , repo_name string ) int {
235
+ response := http.fetch (
236
+ method: .get
237
+ cookies: {
238
+ 'token' : token
239
+ }
240
+ url: prepare_url ('api/v1/$username /$repo_name /issues/count' )
241
+ ) or { exit_with_message (err.str ()) }
242
+
243
+ response_json := json.decode (api.ApiIssueCount, response.body) or {
244
+ exit_with_message (err.str ())
105
245
}
246
+
247
+ return response_json.result
248
+ }
249
+
250
+ fn get_repo_branch_count (token string , username string , repo_name string ) int {
251
+ response := http.fetch (
252
+ method: .get
253
+ cookies: {
254
+ 'token' : token
255
+ }
256
+ url: prepare_url ('api/v1/$username /$repo_name /branches/count' )
257
+ ) or { exit_with_message (err.str ()) }
258
+
259
+ response_json := json.decode (api.ApiBranchCount, response.body) or {
260
+ exit_with_message (err.str ())
261
+ }
262
+
263
+ return response_json.result
106
264
}
0 commit comments