-
-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathadmin_test.v
More file actions
96 lines (87 loc) · 2.27 KB
/
Copy pathadmin_test.v
File metadata and controls
96 lines (87 loc) · 2.27 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
module main
import config
import os
import time
struct AdminEditResult {
succeeded bool
}
fn admin_test_config(db_path string) config.Config {
return config.Config{
repo_storage_path: os.temp_dir()
archive_path: os.temp_dir()
avatars_path: os.temp_dir()
sqlite: config.SqliteConfig{
path: db_path
}
}
}
fn remove_admin_test_db(db_path string) {
for suffix in ['', '-shm', '-wal'] {
os.rm(db_path + suffix) or {}
}
}
fn add_admin_test_user(mut app App, id int, username string) ! {
user := User{
id: id
username: username
created_at: time.now()
is_registered: true
is_admin: true
}
app.add_user(user)!
}
fn concurrently_deactivate_admin(db_path string, user_id int, start chan bool) AdminEditResult {
_ := <-start
conf := admin_test_config(db_path)
mut app := App{
db: connect_db(conf) or { return AdminEditResult{} }
config: conf
}
defer {
app.db.close() or {}
}
app.edit_user(user_id, false, false, false) or { return AdminEditResult{} }
return AdminEditResult{
succeeded: true
}
}
fn test_admin_edits_cannot_concurrently_remove_every_active_admin() {
$if sqlite ? {
db_path := os.join_path(os.temp_dir(), 'gitly_admin_edit_${os.getpid()}.sqlite')
remove_admin_test_db(db_path)
defer {
remove_admin_test_db(db_path)
}
conf := admin_test_config(db_path)
mut app := App{
db: connect_db(conf)!
config: conf
}
defer {
app.db.close() or {}
}
app.create_tables()!
add_admin_test_user(mut app, 1, 'alice')!
add_admin_test_user(mut app, 2, 'bob')!
start := chan bool{cap: 2}
worker_a := spawn concurrently_deactivate_admin(db_path, 1, start)
worker_b := spawn concurrently_deactivate_admin(db_path, 2, start)
start <- true
start <- true
result_a := worker_a.wait()
result_b := worker_b.wait()
assert result_a.succeeded || result_b.succeeded
assert !(result_a.succeeded && result_b.succeeded)
assert app.count_admin_users() == 1
remaining := sql app.db {
select from User where is_admin == true && is_registered == true && is_blocked == false
}!
assert remaining.len == 1
mut rejected := false
app.edit_user(remaining[0].id, false, true, true) or { rejected = true }
assert rejected
assert app.count_admin_users() == 1
} $else {
assert true
}
}