-
Notifications
You must be signed in to change notification settings - Fork 641
/
Copy pathteam.rs
458 lines (377 loc) · 15.4 KB
/
team.rs
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
use crate::{
add_team_to_crate,
builders::{CrateBuilder, PublishBuilder},
new_team, OwnerTeamsResponse, RequestHelper, TestApp,
};
use crates_io::{
models::{Crate, NewTeam},
schema::teams,
};
use diesel::*;
use http::StatusCode;
impl crate::util::MockAnonymousUser {
/// List the team owners of the specified crate.
fn crate_owner_teams(&self, krate_name: &str) -> crate::util::Response<OwnerTeamsResponse> {
let url = format!("/api/v1/crates/{krate_name}/owner_team");
self.get(&url)
}
}
/// Test adding team without `github:`
#[test]
fn not_github() {
let (app, _, user, token) = TestApp::init().with_token();
app.db(|conn| {
CrateBuilder::new("foo_not_github", user.as_model().id).expect_build(conn);
});
let response = token.add_named_owner("foo_not_github", "dropbox:foo:foo");
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response.into_json(),
json!({ "errors": [{ "detail": "unknown organization handler, only 'github:org:team' is supported" }] })
);
}
#[test]
fn weird_name() {
let (app, _, user, token) = TestApp::init().with_token();
app.db(|conn| {
CrateBuilder::new("foo_weird_name", user.as_model().id).expect_build(conn);
});
let response = token.add_named_owner("foo_weird_name", "github:foo/../bar:wut");
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response.into_json(),
json!({ "errors": [{ "detail": "organization cannot contain special characters like /" }] })
);
}
/// Test adding team without second `:`
#[test]
fn one_colon() {
let (app, _, user, token) = TestApp::init().with_token();
app.db(|conn| {
CrateBuilder::new("foo_one_colon", user.as_model().id).expect_build(conn);
});
let response = token.add_named_owner("foo_one_colon", "github:foo");
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response.into_json(),
json!({ "errors": [{ "detail": "missing github team argument; format is github:org:team" }] })
);
}
#[test]
fn add_nonexistent_team() {
let (app, _, user, token) = TestApp::init().with_token();
app.db(|conn| {
CrateBuilder::new("foo_add_nonexistent", user.as_model().id).expect_build(conn);
});
let response =
token.add_named_owner("foo_add_nonexistent", "github:test-org:this-does-not-exist");
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response.into_json(),
json!({ "errors": [{ "detail": "could not find the github team test-org/this-does-not-exist" }] })
);
}
/// Test adding a renamed team
#[test]
fn add_renamed_team() {
let (app, anon) = TestApp::init().empty();
let user = app.db_new_user("user-all-teams");
let token = user.db_new_token("arbitrary token name");
let owner_id = user.as_model().id;
app.db(|conn| {
use crates_io::schema::teams::dsl::*;
CrateBuilder::new("foo_renamed_team", owner_id).expect_build(conn);
// create team with same ID and different name compared to http mock
// used for `add_named_owner`
NewTeam::new(
"github:test-org:old-core", // different team name
1000, // same org ID
2001, // same team id as `core` team
None,
None,
)
.create_or_update(conn)
.unwrap();
assert_eq!(teams.count().get_result::<i64>(conn).unwrap(), 1);
});
token
.add_named_owner("foo_renamed_team", "github:test-org:core")
.good();
let json = anon.crate_owner_teams("foo_renamed_team").good();
assert_eq!(json.teams.len(), 1);
assert_eq!(json.teams[0].login, "github:test-org:core");
}
/// Test adding team names with mixed case, when on the team
#[test]
fn add_team_mixed_case() {
let (app, anon) = TestApp::init().empty();
let user = app.db_new_user("user-all-teams");
let token = user.db_new_token("arbitrary token name");
app.db(|conn| {
CrateBuilder::new("foo_mixed_case", user.as_model().id).expect_build(conn);
});
token
.add_named_owner("foo_mixed_case", "github:Test-Org:Core")
.good();
app.db(|conn| {
let krate: Crate = Crate::by_name("foo_mixed_case").first(conn).unwrap();
let owners = krate.owners(conn).unwrap();
assert_eq!(owners.len(), 2);
let owner = &owners[1];
assert_eq!(owner.login(), owner.login().to_lowercase());
});
let json = anon.crate_owner_teams("foo_mixed_case").good();
assert_eq!(json.teams.len(), 1);
assert_eq!(json.teams[0].login, "github:test-org:core");
}
#[test]
fn add_team_as_org_owner() {
let (app, anon) = TestApp::init().empty();
let user = app.db_new_user("user-org-owner");
let token = user.db_new_token("arbitrary token name");
app.db(|conn| {
CrateBuilder::new("foo_org_owner", user.as_model().id).expect_build(conn);
});
token
.add_named_owner("foo_org_owner", "github:test-org:core")
.good();
app.db(|conn| {
let krate: Crate = Crate::by_name("foo_org_owner").first(conn).unwrap();
let owners = krate.owners(conn).unwrap();
assert_eq!(owners.len(), 2);
let owner = &owners[1];
assert_eq!(owner.login(), owner.login().to_lowercase());
});
let json = anon.crate_owner_teams("foo_org_owner").good();
assert_eq!(json.teams.len(), 1);
assert_eq!(json.teams[0].login, "github:test-org:core");
}
/// Test adding team as owner when not on it
#[test]
fn add_team_as_non_member() {
let (app, _) = TestApp::init().empty();
let user = app.db_new_user("user-one-team");
let token = user.db_new_token("arbitrary token name");
app.db(|conn| {
CrateBuilder::new("foo_team_non_member", user.as_model().id).expect_build(conn);
});
let response = token.add_named_owner("foo_team_non_member", "github:test-org:core");
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response.into_json(),
json!({ "errors": [{ "detail": "only members of a team or organization owners can add it as an owner" }] })
);
}
#[test]
fn remove_team_as_named_owner() {
let (app, _) = TestApp::full().empty();
let username = "user-all-teams";
let user_on_both_teams = app.db_new_user(username);
let token_on_both_teams = user_on_both_teams.db_new_token("arbitrary token name");
app.db(|conn| {
CrateBuilder::new("foo_remove_team", user_on_both_teams.as_model().id).expect_build(conn);
});
token_on_both_teams
.add_named_owner("foo_remove_team", "github:test-org:core")
.good();
// Removing the individual owner is not allowed, since team members don't
// have permission to manage ownership
let response = token_on_both_teams.remove_named_owner("foo_remove_team", username);
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response.into_json(),
json!({ "errors": [{ "detail": "cannot remove all individual owners of a crate. Team member don't have permission to modify owners, so at least one individual owner is required." }] })
);
token_on_both_teams
.remove_named_owner("foo_remove_team", "github:test-org:core")
.good();
let user_on_one_team = app.db_new_user("user-one-team");
let crate_to_publish = PublishBuilder::new("foo_remove_team", "2.0.0");
let response = user_on_one_team.publish_crate(crate_to_publish);
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response.into_json(),
json!({ "errors": [{ "detail": "this crate exists but you don't seem to be an owner. If you believe this is a mistake, perhaps you need to accept an invitation to be an owner before publishing." }] })
);
}
#[test]
fn remove_team_as_team_owner() {
let (app, _) = TestApp::init().empty();
let user_on_both_teams = app.db_new_user("user-all-teams");
let token_on_both_teams = user_on_both_teams.db_new_token("arbitrary token name");
app.db(|conn| {
CrateBuilder::new("foo_remove_team_owner", user_on_both_teams.as_model().id)
.expect_build(conn);
});
token_on_both_teams
.add_named_owner("foo_remove_team_owner", "github:test-org:all")
.good();
let user_on_one_team = app.db_new_user("user-one-team");
let token_on_one_team = user_on_one_team.db_new_token("arbitrary token name");
let response =
token_on_one_team.remove_named_owner("foo_remove_team_owner", "github:test-org:all");
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response.into_json(),
json!({ "errors": [{ "detail": "team members don't have permission to modify owners" }] })
);
let user_org_owner = app.db_new_user("user-org-owner");
let token_org_owner = user_org_owner.db_new_token("arbitrary token name");
let response =
token_org_owner.remove_named_owner("foo_remove_team_owner", "github:test-org:all");
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response.into_json(),
json!({ "errors": [{ "detail": "only owners have permission to modify owners" }] })
);
}
#[test]
fn remove_nonexistent_team() {
let (app, _, user, token) = TestApp::init().with_token();
app.db(|conn| {
CrateBuilder::new("foo_remove_nonexistent", user.as_model().id).expect_build(conn);
insert_into(teams::table)
.values((
teams::login.eq("github:test-org:this-does-not-exist"),
teams::github_id.eq(5678),
))
.execute(conn)
.expect("couldn't insert nonexistent team")
});
token
.remove_named_owner(
"foo_remove_nonexistent",
"github:test-org:this-does-not-exist",
)
.good();
}
/// Test trying to publish a crate we don't own
#[test]
fn publish_not_owned() {
let (app, _) = TestApp::full().empty();
let user_on_both_teams = app.db_new_user("user-all-teams");
let token_on_both_teams = user_on_both_teams.db_new_token("arbitrary token name");
app.db(|conn| {
CrateBuilder::new("foo_not_owned", user_on_both_teams.as_model().id).expect_build(conn);
});
token_on_both_teams
.add_named_owner("foo_not_owned", "github:test-org:core")
.good();
let user_on_one_team = app.db_new_user("user-one-team");
let crate_to_publish = PublishBuilder::new("foo_not_owned", "2.0.0");
let response = user_on_one_team.publish_crate(crate_to_publish);
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response.into_json(),
json!({ "errors": [{ "detail": "this crate exists but you don't seem to be an owner. If you believe this is a mistake, perhaps you need to accept an invitation to be an owner before publishing." }] })
);
}
#[test]
fn publish_org_owner_owned() {
let (app, _) = TestApp::full().empty();
let user_on_both_teams = app.db_new_user("user-all-teams");
let token_on_both_teams = user_on_both_teams.db_new_token("arbitrary token name");
app.db(|conn| {
CrateBuilder::new("foo_not_owned", user_on_both_teams.as_model().id).expect_build(conn);
});
token_on_both_teams
.add_named_owner("foo_not_owned", "github:test-org:core")
.good();
let user_org_owner = app.db_new_user("user-org-owner");
let crate_to_publish = PublishBuilder::new("foo_not_owned", "2.0.0");
let response = user_org_owner.publish_crate(crate_to_publish);
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response.into_json(),
json!({ "errors": [{ "detail": "this crate exists but you don't seem to be an owner. If you believe this is a mistake, perhaps you need to accept an invitation to be an owner before publishing." }] })
);
}
/// Test trying to publish a krate we do own (but only because of teams)
#[test]
fn publish_owned() {
let (app, _) = TestApp::full().empty();
let user_on_both_teams = app.db_new_user("user-all-teams");
let token_on_both_teams = user_on_both_teams.db_new_token("arbitrary token name");
app.db(|conn| {
CrateBuilder::new("foo_team_owned", user_on_both_teams.as_model().id).expect_build(conn);
});
token_on_both_teams
.add_named_owner("foo_team_owned", "github:test-org:all")
.good();
let user_on_one_team = app.db_new_user("user-one-team");
let crate_to_publish = PublishBuilder::new("foo_team_owned", "2.0.0");
user_on_one_team.publish_crate(crate_to_publish).good();
}
/// Test trying to change owners (when only on an owning team)
#[test]
fn add_owners_as_org_owner() {
let (app, _) = TestApp::init().empty();
let user_on_both_teams = app.db_new_user("user-all-teams");
let token_on_both_teams = user_on_both_teams.db_new_token("arbitrary token name");
app.db(|conn| {
CrateBuilder::new("foo_add_owner", user_on_both_teams.as_model().id).expect_build(conn);
});
token_on_both_teams
.add_named_owner("foo_add_owner", "github:test-org:all")
.good();
let user_org_owner = app.db_new_user("user-org-owner");
let token_org_owner = user_org_owner.db_new_token("arbitrary token name");
let response = token_org_owner.add_named_owner("foo_add_owner", "arbitrary_username");
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response.into_json(),
json!({ "errors": [{ "detail": "only owners have permission to modify owners" }] })
);
}
#[test]
fn add_owners_as_team_owner() {
let (app, _) = TestApp::init().empty();
let user_on_both_teams = app.db_new_user("user-all-teams");
let token_on_both_teams = user_on_both_teams.db_new_token("arbitrary token name");
app.db(|conn| {
CrateBuilder::new("foo_add_owner", user_on_both_teams.as_model().id).expect_build(conn);
});
token_on_both_teams
.add_named_owner("foo_add_owner", "github:test-org:all")
.good();
let user_on_one_team = app.db_new_user("user-one-team");
let token_on_one_team = user_on_one_team.db_new_token("arbitrary token name");
let response = token_on_one_team.add_named_owner("foo_add_owner", "arbitrary_username");
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response.into_json(),
json!({ "errors": [{ "detail": "team members don't have permission to modify owners" }] })
);
}
#[test]
fn crates_by_team_id() {
let (app, anon, user) = TestApp::init().with_user();
let user = user.as_model();
let team = app.db(|conn| {
let t = new_team("github:test-org:team")
.create_or_update(conn)
.unwrap();
let krate = CrateBuilder::new("foo", user.id).expect_build(conn);
add_team_to_crate(&t, &krate, user, conn).unwrap();
t
});
let json = anon.search(&format!("team_id={}", team.id));
assert_eq!(json.crates.len(), 1);
}
#[test]
fn crates_by_team_id_not_including_deleted_owners() {
let (app, anon) = TestApp::init().empty();
let user = app.db_new_user("user-all-teams");
let user = user.as_model();
let team = app.db(|conn| {
let t = NewTeam::new("github:test-org:core", 1000, 2001, None, None)
.create_or_update(conn)
.unwrap();
let krate = CrateBuilder::new("foo", user.id).expect_build(conn);
add_team_to_crate(&t, &krate, user, conn).unwrap();
krate.owner_remove(conn, &t.login).unwrap();
t
});
let json = anon.search(&format!("team_id={}", team.id));
assert_eq!(json.crates.len(), 0);
}