| @@ -103,3 +103,188 @@ struct AdminQueueSectionTests { |
| 103 | #expect(AdminQueues.relative("soon") == "soon") |
103 | #expect(AdminQueues.relative("soon") == "soon") |
| 104 | } |
104 | } |
| 105 | } |
105 | } |
| |
106 | |
| |
107 | private func user(_ name: String, state: String, admin: Bool = false) throws -> AdminUser { |
| |
108 | let json = """ |
| |
109 | {"username":"\(name)","state":"\(state)","admin":\(admin),\ |
| |
110 | "created_at":"2026-08-28T21:55:00.752Z"} |
| |
111 | """ |
| |
112 | return try GitbayClient.decoder().decode(AdminUser.self, from: Data(json.utf8)) |
| |
113 | } |
| |
114 | |
| |
115 | struct AdminUserActionTests { |
| |
116 | |
| |
117 | @Test func anAdminIsDemotedAndCanBeDisabled() throws { |
| |
118 | let u = try user("cmc", state: "active", admin: true) |
| |
119 | #expect(u.roleAction == .demote) |
| |
120 | #expect(u.canChangeRole) |
| |
121 | #expect(u.accessAction == .disable) |
| |
122 | } |
| |
123 | |
| |
124 | @Test func anActiveAccountCanBePromoted() throws { |
| |
125 | let u = try user("blotter-ci", state: "active") |
| |
126 | #expect(u.roleAction == .promote) |
| |
127 | #expect(u.canChangeRole) |
| |
128 | } |
| |
129 | |
| |
130 | /// The web draws Promote on every non-admin row but disables it for |
| |
131 | /// an account that is not active. |
| |
132 | @Test func onlyAnActiveAccountCanBePromoted() throws { |
| |
133 | #expect(try user("new", state: "pending").canChangeRole == false) |
| |
134 | #expect(try user("gone", state: "disabled").canChangeRole == false) |
| |
135 | } |
| |
136 | |
| |
137 | @Test func aDisabledAccountIsEnabledAndAnyOtherDisabled() throws { |
| |
138 | #expect(try user("gone", state: "disabled").accessAction == .enable) |
| |
139 | #expect(try user("new", state: "pending").accessAction == .disable) |
| |
140 | } |
| |
141 | |
| |
142 | @Test func demoteAndDisableAskForTheTypedName() { |
| |
143 | #expect(AdminAction.demote.needsTypedName) |
| |
144 | #expect(AdminAction.disable.needsTypedName) |
| |
145 | #expect(!AdminAction.promote.needsTypedName) |
| |
146 | #expect(!AdminAction.enable.needsTypedName) |
| |
147 | } |
| |
148 | |
| |
149 | @Test func eachActionIsItsAdminUserCommand() { |
| |
150 | #expect(AdminAction.promote.argv("x") == ["admin", "user", "promote", "x"]) |
| |
151 | #expect(AdminAction.demote.argv("x") == ["admin", "user", "demote", "x"]) |
| |
152 | #expect(AdminAction.disable.argv("x") == ["admin", "user", "disable", "x"]) |
| |
153 | #expect(AdminAction.enable.argv("x") == ["admin", "user", "enable", "x"]) |
| |
154 | } |
| |
155 | |
| |
156 | @Test func theFiltersAreTheWebsFive() { |
| |
157 | #expect(AdminUserFilter.allCases.map(\.label) == ["All", "Active", "Pending", "Disabled", "Admins"]) |
| |
158 | #expect(AdminUserFilter.all.flags().isEmpty) |
| |
159 | #expect(AdminUserFilter.admin.flags() == ["--state", "admin"]) |
| |
160 | } |
| |
161 | } |
| |
162 | |
| |
163 | @MainActor |
| |
164 | struct AdminCheckTests { |
| |
165 | |
| |
166 | @Test func whoamisAdminFlagDecides() async throws { |
| |
167 | let (client, stub) = try makeClient() |
| |
168 | stub.enqueue(.init(status: 200, json: |
| |
169 | #"{"protocol_version":1,"data":{"username":"cmc","admin":true,"key_scope":"full"}}"#)) |
| |
170 | #expect(await AdminCheck.isAdmin(client)) |
| |
171 | #expect(stub.seen.first?.url.query() == "argv=whoami") |
| |
172 | } |
| |
173 | |
| |
174 | @Test func aNonAdminIsNot() async throws { |
| |
175 | let (client, stub) = try makeClient() |
| |
176 | stub.enqueue(.init(status: 200, json: |
| |
177 | #"{"protocol_version":1,"data":{"username":"ios-smoke","admin":false}}"#)) |
| |
178 | #expect(await AdminCheck.isAdmin(client) == false) |
| |
179 | } |
| |
180 | |
| |
181 | /// The menu just leaves the entry out when the read fails. |
| |
182 | @Test func aFailedReadIsNotAnAdmin() async throws { |
| |
183 | let (client, stub) = try makeClient() |
| |
184 | stub.enqueue(.init(status: 500, json: #"{"protocol_version":1,"error":"boom","exit_code":1}"#)) |
| |
185 | #expect(await AdminCheck.isAdmin(client) == false) |
| |
186 | } |
| |
187 | } |
| |
188 | |
| |
189 | @MainActor |
| |
190 | struct AdminOverviewViewModelTests { |
| |
191 | |
| |
192 | @Test func readsDashboardIntoSections() async throws { |
| |
193 | let (client, stub) = try makeClient() |
| |
194 | stub.enqueue(.init(status: 200, json: adminDashboardJSON)) |
| |
195 | let model = AdminOverviewViewModel(client: client) |
| |
196 | |
| |
197 | await model.load() |
| |
198 | |
| |
199 | let overview = try #require(model.state.value) |
| |
200 | #expect(overview.commit == "db7503f06f11") |
| |
201 | #expect(overview.sections.count == 6) |
| |
202 | #expect(stub.seen.first?.url.query() == "argv=dashboard") |
| |
203 | } |
| |
204 | |
| |
205 | @Test func aDashboardWithoutQueuesIsAnEmptyState() async throws { |
| |
206 | let (client, stub) = try makeClient() |
| |
207 | stub.enqueue(.init(status: 200, json: #"{"protocol_version":1,"data":{"unread":0},"exit_code":0}"#)) |
| |
208 | let model = AdminOverviewViewModel(client: client) |
| |
209 | |
| |
210 | await model.load() |
| |
211 | |
| |
212 | guard case .empty(let message) = model.state else { |
| |
213 | Issue.record("expected .empty, got \(model.state)") |
| |
214 | return |
| |
215 | } |
| |
216 | #expect(message == "Only instance admins can see the worker queues.") |
| |
217 | } |
| |
218 | } |
| |
219 | |
| |
220 | private let userPageJSON = """ |
| |
221 | {"protocol_version":1,"data":{"items":[\ |
| |
222 | {"username":"apple-review","state":"active","admin":false,"created_at":"2026-08-28T21:55:00.752Z",\ |
| |
223 | "last_seen":"2026-09-21T07:05:26.201Z"},\ |
| |
224 | {"username":"bhargavkk","state":"pending","admin":false,"created_at":"2026-09-11T01:50:04.063Z"}],\ |
| |
225 | "next":"YWRtaW4tdXNlcjpiaGFyZ2F2a2s"},"exit_code":0} |
| |
226 | """ |
| |
227 | |
| |
228 | @MainActor |
| |
229 | struct AdminUsersViewModelTests { |
| |
230 | |
| |
231 | @Test func listsEveryAccountByDefault() async throws { |
| |
232 | let (client, stub) = try makeClient() |
| |
233 | stub.enqueue(.init(status: 200, json: userPageJSON)) |
| |
234 | let model = AdminUsersViewModel(client: client) |
| |
235 | |
| |
236 | await model.load() |
| |
237 | |
| |
238 | let users = try #require(model.list.state.value) |
| |
239 | #expect(users.map(\.username) == ["apple-review", "bhargavkk"]) |
| |
240 | #expect(users[0].lastSeen != nil) |
| |
241 | #expect(users[1].lastSeen == nil) |
| |
242 | #expect(model.list.hasMore) |
| |
243 | #expect(stub.seen.first?.url.query() == "argv=admin&argv=user&argv=list&argv=--limit&argv=50") |
| |
244 | } |
| |
245 | |
| |
246 | @Test func aFilterReloadsWithItsState() async throws { |
| |
247 | let (client, stub) = try makeClient() |
| |
248 | stub.enqueue(.init(status: 200, json: userPageJSON)) |
| |
249 | let model = AdminUsersViewModel(client: client) |
| |
250 | await model.load() |
| |
251 | |
| |
252 | stub.enqueue(.init(status: 200, json: #"{"protocol_version":1,"data":{"items":[]},"exit_code":0}"#)) |
| |
253 | model.filter = .disabled |
| |
254 | await until { |
| |
255 | if case .empty = model.list.state { return true } |
| |
256 | return false |
| |
257 | } |
| |
258 | |
| |
259 | #expect(stub.seen.last?.url.query() |
| |
260 | == "argv=admin&argv=user&argv=list&argv=--state&argv=disabled&argv=--limit&argv=50") |
| |
261 | guard case .empty(let message) = model.list.state else { return } |
| |
262 | #expect(message == "No account matches") |
| |
263 | } |
| |
264 | |
| |
265 | @Test func anActionSendsItsCommandAndReloads() async throws { |
| |
266 | let (client, stub) = try makeClient() |
| |
267 | stub.enqueue(.init(status: 200, json: #"{"protocol_version":1,"exit_code":0}"#, match: "cmd")) |
| |
268 | stub.enqueue(.init(status: 200, json: userPageJSON, match: "argv=list")) |
| |
269 | let model = AdminUsersViewModel(client: client) |
| |
270 | |
| |
271 | await model.perform(.disable, on: "bhargavkk") |
| |
272 | |
| |
273 | let write = try #require(stub.seen.first { $0.method == "POST" }) |
| |
274 | let body = try #require(try JSONSerialization.jsonObject(with: write.body) as? [String: Any]) |
| |
275 | #expect(body["argv"] as? [String] == ["admin", "user", "disable", "bhargavkk"]) |
| |
276 | #expect(model.actionError == nil) |
| |
277 | #expect(model.list.state.value?.count == 2) |
| |
278 | } |
| |
279 | |
| |
280 | @Test func aRefusalIsShownVerbatim() async throws { |
| |
281 | let (client, stub) = try makeClient() |
| |
282 | stub.enqueue(.init(status: 200, json: |
| |
283 | #"{"protocol_version":1,"error":"cmc is the last admin","exit_code":4}"#, match: "cmd")) |
| |
284 | let model = AdminUsersViewModel(client: client) |
| |
285 | |
| |
286 | await model.perform(.demote, on: "cmc") |
| |
287 | |
| |
288 | #expect(model.actionError == "cmc is the last admin") |
| |
289 | } |
| |
290 | } |