types.rs 31.7 KB
Newer Older
1
2
3
4
5
6
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

Ben Boeckel's avatar
Ben Boeckel committed
7
8
use std::fs::File;

9
use crates::chrono::{DateTime, NaiveDate, TimeZone, Utc};
10
use crates::itertools;
11
use crates::serde::de::DeserializeOwned;
12
use crates::serde_json::{from_reader, json};
13

14
use types::*;
15

16
17
18
19
20
21
fn check_user_brad_king(user: &UserBasic) {
    assert_eq!(user.username, "brad.king");
    assert_eq!(user.web_url, "https://gitlab.kitware.com/brad.king");
    assert_eq!(user.name, "Brad King");
    assert_eq!(user.state, UserState::Active);
    assert_eq!(
22
23
        user.avatar_url.as_ref().unwrap(),
        "https://secure.gravatar.com/avatar/0617392a2f9fd505720d0c42cefc1a10?s=80&d=identicon",
24
25
26
27
28
29
30
31
32
33
    );
    assert_eq!(user.id, UserId::new(10));
}

fn check_user_ben_boeckel(user: &UserBasic) {
    assert_eq!(user.username, "ben.boeckel");
    assert_eq!(user.web_url, "https://gitlab.kitware.com/ben.boeckel");
    assert_eq!(user.name, "Ben Boeckel");
    assert_eq!(user.state, UserState::Active);
    assert_eq!(
34
35
        user.avatar_url.as_ref().unwrap(),
        "https://secure.gravatar.com/avatar/2f5f7e99190174edb5a2f66b8653b0b2?s=80&d=identicon",
36
37
38
39
40
41
42
43
44
45
    );
    assert_eq!(user.id, UserId::new(13));
}

fn check_user_kwrobot(user: &UserBasic) {
    assert_eq!(user.username, "kwrobot");
    assert_eq!(user.web_url, "https://gitlab.kitware.com/kwrobot");
    assert_eq!(user.name, "Kitware Robot");
    assert_eq!(user.state, UserState::Active);
    assert_eq!(
46
47
        user.avatar_url.as_ref().unwrap(),
        "https://secure.gravatar.com/avatar/9ddcd45fcb89d966aab95b1f1002f84c?s=80&d=identicon",
48
49
50
51
52
53
    );
    assert_eq!(user.id, UserId::new(11));
}

fn check_user_buildbot(user: &UserBasic) {
    assert_eq!(
54
55
        user.avatar_url.as_ref().unwrap(),
        "https://gitlab.kitware.com/uploads/-/system/user/avatar/35/buildbot-logo.png",
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
    );
    assert_eq!(user.id, UserId::new(35));
    assert_eq!(user.name, "buildbot");
    assert_eq!(user.username, "buildbot");
    assert_eq!(user.state, UserState::Active);
    assert_eq!(user.web_url, "https://gitlab.kitware.com/buildbot");
}

fn check_namespace_utils(namespace: &Namespace) {
    assert_eq!(namespace.name, "Utils");
    assert_eq!(namespace.path, "utils");
    assert_eq!(namespace.id(), NamespaceId::Group(GroupId::new(498)));
    assert_eq!(namespace.kind, NamespaceKind::Group);
    assert_eq!(namespace.full_path, "utils");
    assert_eq!(namespace.avatar_url, None);
    assert_eq!(namespace.web_url, "https://gitlab.kitware.com/groups/utils");
    assert_eq!(namespace.members_count_with_descendants, None);
}

fn check_empty_time_stats(time_stats: &IssuableTimeStats) {
    assert_eq!(time_stats.time_estimate, 0);
    assert_eq!(time_stats.total_time_spent, 0);
    assert_eq!(time_stats.human_time_estimate, None);
    assert_eq!(time_stats.human_total_time_spent, None);
}

82
83
84
85
86
fn datetime(ymd: (i32, u32, u32), time: (u32, u32, u32, u32)) -> DateTime<Utc> {
    Utc.ymd(ymd.0, ymd.1, ymd.2)
        .and_hms_milli(time.0, time.1, time.2, time.3)
}

87
fn read_test_file<T: DeserializeOwned>(name: &str) -> T {
Ben Boeckel's avatar
Ben Boeckel committed
88
89
90
91
92
    let fin = File::open(format!(
        concat!(env!("CARGO_MANIFEST_DIR"), "/data/{}.json"),
        name,
    ))
    .unwrap();
93
94
95
96
97

    from_reader::<File, T>(fin).unwrap()
}

#[test]
98
99
fn test_read_award_emoji() {
    let award_emoji: AwardEmoji = read_test_file("award_emoji");
100

101
102
    assert_eq!(award_emoji.id, AwardId::new(335));
    assert_eq!(award_emoji.name, "8ball");
103
    check_user_ben_boeckel(&award_emoji.user);
Ben Boeckel's avatar
Ben Boeckel committed
104
105
    assert_eq!(
        award_emoji.created_at,
106
        datetime((2016, 12, 7), (16, 23, 46, 742)),
Ben Boeckel's avatar
Ben Boeckel committed
107
108
109
    );
    assert_eq!(
        award_emoji.updated_at,
110
        datetime((2016, 12, 7), (16, 23, 46, 742)),
Ben Boeckel's avatar
Ben Boeckel committed
111
112
113
    );
    assert_eq!(
        award_emoji.awardable_id(),
114
        AwardableId::Note(NoteId::new(177_359)),
Ben Boeckel's avatar
Ben Boeckel committed
115
    );
116
117
118
119
120
121
122
123
124
125
126
    assert_eq!(award_emoji.awardable_type, AwardableType::Note);
}

#[test]
fn test_read_commit_note() {
    let commit_note: CommitNote = read_test_file("commit_note");

    assert_eq!(commit_note.note, "Example commit note for data fetching.");
    assert_eq!(commit_note.path, None);
    assert_eq!(commit_note.line, None);
    assert_eq!(commit_note.line_type, None);
127
    check_user_ben_boeckel(&commit_note.author);
Ben Boeckel's avatar
Ben Boeckel committed
128
129
    assert_eq!(
        commit_note.created_at,
130
        datetime((2016, 12, 7), (16, 28, 33, 966)),
Ben Boeckel's avatar
Ben Boeckel committed
131
    );
132
133
134
135
136
137
}

#[test]
fn test_read_commit_status() {
    let commit_status: CommitStatus = read_test_file("commit_status");

138
    assert_eq!(commit_status.id, CommitStatusId::new(931_434));
Ben Boeckel's avatar
Ben Boeckel committed
139
140
    assert_eq!(
        commit_status.sha,
141
        ObjectId::new("de4ac3cf96cb8a0893be22b03f5171d934f9d392"),
Ben Boeckel's avatar
Ben Boeckel committed
142
    );
143
    assert_eq!(commit_status.ref_.as_ref().unwrap(), "master");
144
145
    assert_eq!(commit_status.status, StatusState::Success);
    assert_eq!(commit_status.name, "rust-gitlab-megas-linux-debug");
Ben Boeckel's avatar
Ben Boeckel committed
146
    assert_eq!(
147
148
        commit_status.target_url.as_ref().unwrap(),
        "https://buildbot.kitware.com/builders/rust-gitlab-megas-linux-debug/builds/41",
Ben Boeckel's avatar
Ben Boeckel committed
149
    );
150
    assert_eq!(commit_status.description.as_ref().unwrap(), "expected");
Ben Boeckel's avatar
Ben Boeckel committed
151
152
    assert_eq!(
        commit_status.created_at,
153
        datetime((2016, 11, 8), (14, 35, 32, 627)),
Ben Boeckel's avatar
Ben Boeckel committed
154
    );
155
    assert_eq!(commit_status.started_at, None);
Ben Boeckel's avatar
Ben Boeckel committed
156
    assert_eq!(
157
158
        commit_status.finished_at.unwrap(),
        datetime((2016, 11, 8), (14, 35, 32, 629)),
Ben Boeckel's avatar
Ben Boeckel committed
159
    );
160
    assert_eq!(commit_status.allow_failure, false);
161
    check_user_buildbot(&commit_status.author);
162
    assert_eq!(commit_status.coverage, None);
163
164
165
166
167
168
169
}

#[test]
fn test_read_issue() {
    let issue: Issue = read_test_file("issue");

    assert_eq!(issue.id, IssueId::new(69328));
Brad King's avatar
Brad King committed
170
    assert_eq!(issue.iid, IssueInternalId::new(6));
171
172
    assert_eq!(issue.project_id, ProjectId::new(855));
    assert_eq!(issue.title, "fix documentation warnings");
173
    assert_eq!(issue.description, Some(String::new()));
174
    assert_eq!(issue.state, IssueState::Closed);
Ben Boeckel's avatar
Ben Boeckel committed
175
176
    assert_eq!(
        issue.created_at,
177
        datetime((2016, 10, 30), (18, 54, 28, 954)),
Ben Boeckel's avatar
Ben Boeckel committed
178
    );
179
    assert_eq!(issue.updated_at, datetime((2019, 7, 17), (13, 53, 48, 869)));
Brad King's avatar
Brad King committed
180
    assert_eq!(issue.closed_at, None);
Brad King's avatar
Brad King committed
181
    assert!(issue.closed_by.is_none());
182
    itertools::assert_equal(&issue.labels, &["area:doc"]);
183
    assert!(issue.milestone.is_none());
184
    check_user_ben_boeckel(&issue.author);
185
186
187
188
189
190
    let assignee = issue.assignee.as_ref().unwrap();
    check_user_ben_boeckel(assignee);
    let assignees = issue.assignees.as_ref().unwrap();
    assert_eq!(assignees.len(), 1);
    let assignee = &assignees[0];
    check_user_ben_boeckel(assignee);
Brad King's avatar
Brad King committed
191
    assert_eq!(issue.subscribed, Some(true));
192
    assert_eq!(issue.user_notes_count, 0);
Brad King's avatar
Brad King committed
193
    assert_eq!(issue.merge_requests_count, 1);
194
195
196
    assert_eq!(issue.upvotes, 0);
    assert_eq!(issue.downvotes, 0);
    assert_eq!(issue.due_date, None);
Brad King's avatar
Brad King committed
197
    assert_eq!(issue.has_tasks, Some(false));
198
    assert_eq!(issue.confidential, false);
Brad King's avatar
Brad King committed
199
    assert_eq!(issue.discussion_locked, None);
Ben Boeckel's avatar
Ben Boeckel committed
200
201
202
203
    assert_eq!(
        issue.web_url,
        "https://gitlab.kitware.com/utils/rust-gitlab/issues/6",
    );
Brad King's avatar
Brad King committed
204
    assert!(issue.has_links());
205
206
207
208
209
210
}

#[test]
fn test_read_issue_reference() {
    let issue_reference: IssueReference = read_test_file("issue_reference");

211
212
    let issue = if let IssueReference::Internal(issue) = issue_reference {
        issue
213
214
    } else {
        panic!("expected to have an internal issue reference");
215
216
217
218
219
220
221
    };

    assert_eq!(issue.id, IssueId::new(69075));
    assert_eq!(issue.iid, IssueInternalId::new(5));
    assert_eq!(issue.project_id, ProjectId::new(855));
    assert_eq!(issue.title, "Add project hook APIs");
    assert_eq!(
222
223
224
        issue.description.as_ref().unwrap(),
        "The workflow currently requires that the robot be able to register itself as a webhook \
         for new projects. An API needs added for this.\n\nCc: @brad.king",
225
226
    );
    assert_eq!(issue.state, IssueState::Closed);
227
228
    assert_eq!(issue.created_at, datetime((2016, 10, 4), (18, 59, 37, 178)));
    assert_eq!(issue.updated_at, datetime((2017, 7, 7), (6, 31, 5, 370)));
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
    assert_eq!(issue.closed_at, None);
    assert!(issue.closed_by.is_none());
    assert!(issue.labels.is_empty());
    assert!(issue.milestone.is_none());
    check_user_ben_boeckel(&issue.author);
    let assignee = issue.assignee.as_ref().unwrap();
    check_user_ben_boeckel(assignee);
    assert_eq!(issue.subscribed, None);
    check_empty_time_stats(&issue.time_stats);
    assert_eq!(issue.user_notes_count, 0);
    assert_eq!(issue.merge_requests_count, 1);
    assert_eq!(issue.upvotes, 0);
    assert_eq!(issue.downvotes, 0);
    assert_eq!(issue.due_date, None);
    assert_eq!(issue.confidential, false);
    assert_eq!(issue.discussion_locked, None);
    assert_eq!(
        issue.web_url,
        "https://gitlab.kitware.com/utils/rust-gitlab/issues/5",
    );
249
250
251
252
253
254
255
256
257
258
}

#[test]
fn test_read_member() {
    let member: Member = read_test_file("member");

    assert_eq!(member.username, "kwrobot");
    assert_eq!(member.name, "Kitware Robot");
    assert_eq!(member.id, UserId::new(11));
    assert_eq!(member.state, UserState::Active);
Ben Boeckel's avatar
Ben Boeckel committed
259
    assert_eq!(
260
261
        member.avatar_url.as_ref().unwrap(),
        "https://secure.gravatar.com/avatar/9ddcd45fcb89d966aab95b1f1002f84c?s=80&d=identicon",
Ben Boeckel's avatar
Ben Boeckel committed
262
    );
263
264
265
266
267
    assert_eq!(member.web_url, "https://gitlab.kitware.com/kwrobot");
    assert_eq!(member.access_level, 50);
    assert_eq!(member.expires_at, None);
}

268
fn check_merge_request_a(merge_request: &MergeRequest) {
269
    assert_eq!(merge_request.id, MergeRequestId::new(20215));
Brad King's avatar
Brad King committed
270
    assert_eq!(merge_request.iid, MergeRequestInternalId::new(35));
271
    assert_eq!(merge_request.project_id, ProjectId::new(855));
272
    assert_eq!(merge_request.title, "gitlab: expose hook addition API");
273
    assert_eq!(merge_request.description.as_ref().unwrap(), "Fixes #5.");
274
    assert_eq!(merge_request.state, MergeRequestState::Merged);
Ben Boeckel's avatar
Ben Boeckel committed
275
276
    assert_eq!(
        merge_request.created_at,
277
        datetime((2016, 10, 4), (19, 56, 43, 276)),
Ben Boeckel's avatar
Ben Boeckel committed
278
279
280
    );
    assert_eq!(
        merge_request.updated_at,
281
        datetime((2016, 10, 4), (20, 18, 57, 940)),
Ben Boeckel's avatar
Ben Boeckel committed
282
283
    );
    assert_eq!(
284
285
        merge_request.merged_at.unwrap(),
        datetime((2016, 10, 4), (20, 18, 57, 914)),
Ben Boeckel's avatar
Ben Boeckel committed
286
    );
Brad King's avatar
Brad King committed
287
288
    assert!(merge_request.merged_by.is_none());
    assert!(merge_request.closed_by.is_none());
289
    assert_eq!(merge_request.target_branch, "master");
290
    assert_eq!(merge_request.source_branch, "add_hook-api");
291
292
    assert_eq!(merge_request.upvotes, 0);
    assert_eq!(merge_request.downvotes, 0);
293
    check_user_ben_boeckel(&merge_request.author);
294
295
296
297
298
299
    let assignee = merge_request.assignee.as_ref().unwrap();
    check_user_brad_king(assignee);
    let assignees = merge_request.assignees.as_ref().unwrap();
    assert_eq!(assignees.len(), 1);
    let assignee = &assignees[0];
    check_user_brad_king(assignee);
300
    assert_eq!(merge_request.source_project_id, ProjectId::new(856));
301
302
    assert_eq!(merge_request.target_project_id, ProjectId::new(855));
    assert!(merge_request.labels.is_empty());
303
304
305
}

fn check_merge_request_b(merge_request: &MergeRequest) {
306
307
308
309
    let check_sha = |oid: Option<&ObjectId>, value| {
        assert_eq!(oid.unwrap().value(), value);
    };

310
    assert_eq!(merge_request.work_in_progress, false);
311
312
    assert_eq!(merge_request.allow_collaboration, None);
    assert_eq!(merge_request.allow_maintainer_to_push, None);
313
    assert!(merge_request.milestone.is_none());
Brad King's avatar
Brad King committed
314
    assert_eq!(merge_request.squash, false);
Brad King's avatar
Brad King committed
315
    assert_eq!(merge_request.merge_when_pipeline_succeeds, false);
316
    assert_eq!(merge_request.merge_status, MergeStatus::CanBeMerged);
Brad King's avatar
Brad King committed
317
318
319
320
    check_sha(
        merge_request.sha.as_ref(),
        "04e94ae667024a62a90179f395bfdc2b35f3efd2",
    );
321
    let diff_refs = merge_request.diff_refs.as_ref().unwrap();
Brad King's avatar
Brad King committed
322
323
324
325
326
327
328
329
330
331
332
333
    check_sha(
        diff_refs.base_sha.as_ref(),
        "981262b03fc0149c1677ca51ea47b570e30d6a90",
    );
    check_sha(
        diff_refs.head_sha.as_ref(),
        "04e94ae667024a62a90179f395bfdc2b35f3efd2",
    );
    check_sha(
        diff_refs.start_sha.as_ref(),
        "981262b03fc0149c1677ca51ea47b570e30d6a90",
    );
Brad King's avatar
Brad King committed
334
335
    assert_eq!(merge_request.merge_error, None);
    assert_eq!(merge_request.rebase_in_progress, None);
336
    assert_eq!(merge_request.merge_commit_sha, None);
Brad King's avatar
Brad King committed
337
    assert_eq!(merge_request.subscribed, Some(true));
338
    check_empty_time_stats(&merge_request.time_stats);
Brad King's avatar
Brad King committed
339
    assert_eq!(merge_request.blocking_discussions_resolved, true);
340
    assert_eq!(merge_request.changes_count.as_ref().unwrap(), "3");
341
    assert_eq!(merge_request.user_notes_count, 3);
Brad King's avatar
Brad King committed
342
    assert_eq!(merge_request.discussion_locked, None);
343
344
    assert_eq!(merge_request.should_remove_source_branch, None);
    assert_eq!(merge_request.force_remove_source_branch, Some(true));
Brad King's avatar
Brad King committed
345
    assert_eq!(merge_request.has_conflicts, false);
346
    assert_eq!(merge_request.user.as_ref().unwrap().can_merge, true);
Ben Boeckel's avatar
Ben Boeckel committed
347
348
349
350
    assert_eq!(
        merge_request.web_url,
        "https://gitlab.kitware.com/utils/rust-gitlab/merge_requests/35",
    );
351
352
}

353
354
355
356
357
358
359
360
361
#[test]
fn test_read_merge_request() {
    let merge_request: MergeRequest = read_test_file("merge_request");

    // Split for clippy's complexity checks.
    check_merge_request_a(&merge_request);
    check_merge_request_b(&merge_request);
}

362
363
364
365
#[test]
fn test_read_note() {
    let note: Note = read_test_file("note");

366
    assert_eq!(note.id, NoteId::new(177_373));
Ben Boeckel's avatar
Ben Boeckel committed
367
    assert_eq!(note.body, "Status changed to merged");
368
    assert_eq!(note.attachment, None);
369
    check_user_kwrobot(&note.author);
370
371
    assert_eq!(note.created_at, datetime((2016, 10, 4), (20, 18, 57, 937)));
    assert_eq!(note.updated_at, datetime((2016, 10, 4), (20, 18, 57, 937)));
Brad King's avatar
Brad King committed
372
373
374
    assert_eq!(note.resolvable, false);
    assert_eq!(note.resolved, None);
    assert!(note.resolved_by.is_none());
375
    assert_eq!(note.system, true);
Ben Boeckel's avatar
Ben Boeckel committed
376
    assert_eq!(
377
378
        note.noteable_id().unwrap(),
        NoteableId::MergeRequest(MergeRequestId::new(20215)),
Ben Boeckel's avatar
Ben Boeckel committed
379
380
    );
    assert_eq!(
381
382
        note.noteable_iid().unwrap(),
        NoteableInternalId::MergeRequest(MergeRequestInternalId::new(35)),
Ben Boeckel's avatar
Ben Boeckel committed
383
    );
384
385
386
    assert_eq!(note.noteable_type, NoteType::MergeRequest);
}

387
388
389
#[test]
fn test_read_singlenote_discussion() {
    let discussions: Vec<Discussion> = read_test_file("discussion");
Ben Boeckel's avatar
Ben Boeckel committed
390
391
392
393
    let discussion = discussions
        .iter()
        .find(|x| x.id.value() == "18ea341cb10e952889e277836ba638c6b17ff26c")
        .unwrap();
394
395
396
    assert!(discussion.individual_note);
    assert_eq!(discussion.notes.len(), 1);
    let note = discussion.notes.get(0).unwrap();
397
    assert!(!note.resolvable);
398
    assert!(note.position.is_none());
399
    assert_eq!(note.note_type, None)
400
401
402
403
404
}

#[test]
fn test_read_nocode_discussion() {
    let discussions: Vec<Discussion> = read_test_file("discussion");
Ben Boeckel's avatar
Ben Boeckel committed
405
406
407
408
    let discussion = discussions
        .iter()
        .find(|x| x.id.value() == "a4d5505b3556eaa45edbe567af7aebc1760dedd7")
        .unwrap();
409
    assert!(!discussion.individual_note);
410
411
412
413
414
415
    assert_eq!(discussion.notes.len(), 3);
    let question = discussion.notes.get(0).unwrap();
    let comment = discussion.notes.get(1).unwrap();
    assert!(question.resolvable);
    assert!(comment.resolvable);

416
    assert_eq!(question.resolved, Some(true));
417
418
419
420

    assert!(question.position.is_none());
    assert!(comment.position.is_none());

421
422
    assert_eq!(question.id, NoteId::new(607_911));
    assert_eq!(comment.id, NoteId::new(607_912));
423
424
425
426
427
428
429
430

    assert_eq!(question.note_type, Some(DiscussionNoteType::DiscussionNote));
    assert_eq!(comment.note_type, Some(DiscussionNoteType::DiscussionNote));
}

#[test]
fn test_read_code_discussion() {
    let discussions: Vec<Discussion> = read_test_file("discussion");
Ben Boeckel's avatar
Ben Boeckel committed
431
432
433
434
    let discussion = discussions
        .into_iter()
        .find(|x| x.id.value() == "9f4998b2308728b95cff52af97019479e1269183")
        .unwrap();
435
    assert!(!discussion.individual_note);
436
437
    let note = discussion.notes.get(0).unwrap();
    assert!(note.resolvable);
438
    assert_eq!(note.resolved, Some(true));
439
    check_user_brad_king(&note.author);
440
    assert_eq!(note.id, NoteId::new(619_272));
441
    assert_eq!(note.note_type, Some(DiscussionNoteType::DiffNote));
442
443
444
445
446
447
448
449
    let position = note.position.as_ref().unwrap();
    assert_eq!(position.position_type, NotePositionType::Text);
    assert_eq!(
        position.head_sha.value(),
        "04e94ae667024a62a90179f395bfdc2b35f3efd2",
    );
    assert_eq!(position.new_line, Some(156));
    assert_eq!(position.new_path, "src/gitlab.rs");
450
451
}

452
fn check_project_a(project: &Project) {
453
    assert_eq!(project.id, ProjectId::new(855));
Ben Boeckel's avatar
Ben Boeckel committed
454
    assert_eq!(
455
456
        project.description.as_ref().unwrap(),
        "Rust library for communicating with a Gitlab instance.",
Ben Boeckel's avatar
Ben Boeckel committed
457
    );
458
    assert_eq!(project.default_branch.as_ref().unwrap(), "master");
459
460
    assert!(project.tag_list.is_empty());
    assert_eq!(project.archived, false);
Brad King's avatar
Brad King committed
461
    assert_eq!(project.empty_repo, false);
Brad King's avatar
Brad King committed
462
    assert_eq!(project.visibility, VisibilityLevel::Public);
Ben Boeckel's avatar
Ben Boeckel committed
463
464
465
466
467
468
469
470
471
472
473
474
475
    assert_eq!(
        project.ssh_url_to_repo,
        "git@gitlab.kitware.com:utils/rust-gitlab.git",
    );
    assert_eq!(
        project.http_url_to_repo,
        "https://gitlab.kitware.com/utils/rust-gitlab.git",
    );
    assert_eq!(
        project.web_url,
        "https://gitlab.kitware.com/utils/rust-gitlab",
    );
    assert_eq!(
476
477
        project.readme_url.as_ref().unwrap(),
        "https://gitlab.kitware.com/utils/rust-gitlab/blob/master/README.md",
Ben Boeckel's avatar
Ben Boeckel committed
478
    );
479
480
481
482
483
484
    assert!(project.owner.is_none());
    assert_eq!(project.name, "rust-gitlab");
    assert_eq!(project.name_with_namespace, "Utils / rust-gitlab");
    assert_eq!(project.path, "rust-gitlab");
    assert_eq!(project.path_with_namespace, "utils/rust-gitlab");
    assert_eq!(project.container_registry_enabled, Some(true));
Ben Boeckel's avatar
Ben Boeckel committed
485
486
    assert_eq!(
        project.created_at,
487
        datetime((2016, 6, 29), (17, 35, 12, 495)),
Ben Boeckel's avatar
Ben Boeckel committed
488
489
490
    );
    assert_eq!(
        project.last_activity_at,
491
        datetime((2019, 7, 30), (16, 42, 57, 649)),
Ben Boeckel's avatar
Ben Boeckel committed
492
    );
493
494
495
}

fn check_project_b(project: &Project) {
Brad King's avatar
Brad King committed
496
    assert_eq!(project.import_error, None);
497
498
499
    assert_eq!(project.shared_runners_enabled, true);
    assert_eq!(project.lfs_enabled, true);
    assert_eq!(project.creator_id, UserId::new(13));
500
    check_namespace_utils(&project.namespace);
501
502
    assert!(project.forked_from_project.is_none());
    assert_eq!(project.avatar_url, None);
Brad King's avatar
Brad King committed
503
    assert_eq!(project.ci_config_path, None);
Brad King's avatar
Brad King committed
504
505
506
    assert_eq!(project.star_count, 6);
    assert_eq!(project.forks_count, 17);
    assert_eq!(project.open_issues_count, Some(8));
Brad King's avatar
Brad King committed
507
    assert_eq!(project.public_jobs, true);
508
    assert!(project.shared_with_groups.is_empty());
Brad King's avatar
Brad King committed
509
    assert_eq!(project.only_allow_merge_if_pipeline_succeeds, Some(false));
Ben Boeckel's avatar
Ben Boeckel committed
510
511
512
513
    assert_eq!(
        project.only_allow_merge_if_all_discussions_are_resolved,
        None,
    );
Brad King's avatar
Brad King committed
514
    assert_eq!(project.remove_source_branch_after_merge, None);
Brad King's avatar
Brad King committed
515
    assert_eq!(project.printing_merge_request_link_enabled, Some(true));
Brad King's avatar
Brad King committed
516
    assert_eq!(project.request_access_enabled, false);
Brad King's avatar
Brad King committed
517
    assert_eq!(project.resolve_outdated_diff_discussions, None);
518
519

    assert_eq!(project.jobs_enabled, false);
520
521
522
523
    assert_eq!(project.issues_enabled, true);
    assert_eq!(project.merge_requests_enabled, true);
    assert_eq!(project.snippets_enabled, false);
    assert_eq!(project.wiki_enabled, true);
524
}
525

526
fn check_project_c(project: &Project) {
Ben Boeckel's avatar
Ben Boeckel committed
527
528
529
530
    assert_eq!(
        project.builds_access_level,
        FeatureVisibilityLevel::Disabled,
    );
Brad King's avatar
Brad King committed
531
    assert_eq!(project.issues_access_level, FeatureVisibilityLevel::Enabled);
Ben Boeckel's avatar
Ben Boeckel committed
532
533
534
535
536
537
538
539
540
541
542
543
    assert_eq!(
        project.merge_requests_access_level,
        FeatureVisibilityLevel::Enabled,
    );
    assert_eq!(
        project.repository_access_level,
        FeatureVisibilityLevel::Enabled,
    );
    assert_eq!(
        project.snippets_access_level,
        FeatureVisibilityLevel::Disabled,
    );
Brad King's avatar
Brad King committed
544
545
    assert_eq!(project.wiki_access_level, FeatureVisibilityLevel::Enabled);

546
    assert_eq!(project.merge_method.as_ref().unwrap(), "merge");
547
548
549
550
551
552
    let permissions = project.permissions.as_ref().unwrap();
    let group_access = permissions.group_access.as_ref().unwrap();
    assert_eq!(group_access.access_level, 50);
    assert_eq!(group_access.notification_level, Some(3));
    assert!(permissions.project_access.is_none());
    assert!(project.has_links());
553
554
}

555
556
557
558
559
560
561
562
563
564
#[test]
fn test_read_project() {
    let project: Project = read_test_file("project");

    // Split for clippy's complexity checks.
    check_project_a(&project);
    check_project_b(&project);
    check_project_c(&project);
}

565
566
567
568
#[test]
fn test_read_project_hook() {
    let project_hook: ProjectHook = read_test_file("project_hook");

569
570
    assert_eq!(project_hook.id, HookId::new(1262));
    assert_eq!(project_hook.url, "http://kwrobot02:8082/gitlab.kitware.com");
Ben Boeckel's avatar
Ben Boeckel committed
571
572
    assert_eq!(
        project_hook.created_at,
573
        datetime((2016, 12, 16), (16, 37, 24, 589)),
Ben Boeckel's avatar
Ben Boeckel committed
574
    );
575
    assert_eq!(project_hook.push_events, true);
576
    assert_eq!(project_hook.push_events_branch_filter, None);
577
578
    assert_eq!(project_hook.tag_push_events, true);
    assert_eq!(project_hook.issues_events, true);
579
    assert_eq!(project_hook.confidential_issues_events, Some(true));
580
581
    assert_eq!(project_hook.merge_requests_events, true);
    assert_eq!(project_hook.note_events, true);
582
    assert_eq!(project_hook.confidential_note_events, Some(true));
Brad King's avatar
Brad King committed
583
    assert_eq!(project_hook.repository_update_events, false);
584
    assert_eq!(project_hook.enable_ssl_verification, true);
Brad King's avatar
Brad King committed
585
    assert_eq!(project_hook.job_events, true);
586
587
    assert_eq!(project_hook.pipeline_events, true);
    assert_eq!(project_hook.wiki_page_events, true);
588
589
590
591
592
593
594
}

#[test]
fn test_read_repo_branch() {
    let repo_branch: RepoBranch = read_test_file("repo_branch");

    assert_eq!(repo_branch.name, "master");
595
596
597
598
599
    let commit = repo_branch.commit.as_ref().unwrap();
    assert_eq!(commit.author_email, "brad.king@kitware.com");
    assert_eq!(commit.author_name, "Brad King");
    assert_eq!(
        commit.authored_date,
600
        datetime((2018, 7, 12), (12, 50, 24, 0)),
601
602
603
    );
    assert_eq!(
        commit.committed_date,
604
        datetime((2018, 7, 12), (12, 50, 24, 0)),
605
    );
606
    assert_eq!(commit.created_at, datetime((2018, 7, 12), (12, 50, 24, 0)));
607
608
609
610
611
612
613
614
615
    assert_eq!(commit.committer_email, "brad.king@kitware.com");
    assert_eq!(commit.committer_name, "Brad King");
    assert_eq!(
        commit.id,
        ObjectId::new("e59db4b129b29df220ecec6119ed2130207a0397"),
    );
    assert_eq!(commit.short_id, ObjectId::new("e59db4b1"));
    assert_eq!(commit.title, "cargo: prep for 0.1100.1");
    assert_eq!(commit.message, "cargo: prep for 0.1100.1\n");
616
617
618
    itertools::assert_equal(
        &commit.parent_ids,
        &[ObjectId::new("5c81cc05661dcbb5fd923cca093920816c21ef7e")],
619
    );
620
    assert_eq!(repo_branch.merged, Some(false));
621
622
623
    assert_eq!(repo_branch.protected, Some(true));
    assert_eq!(repo_branch.developers_can_push, Some(false));
    assert_eq!(repo_branch.developers_can_merge, Some(false));
Brad King's avatar
Brad King committed
624
    assert_eq!(repo_branch.can_push, Some(true));
Brad King's avatar
Brad King committed
625
    assert_eq!(repo_branch.default, Some(true));
626
627
628
629
630
631
}

#[test]
fn test_read_repo_commit_detail() {
    let repo_commit_detail: RepoCommitDetail = read_test_file("repo_commit_detail");

Ben Boeckel's avatar
Ben Boeckel committed
632
633
634
635
    assert_eq!(
        repo_commit_detail.id,
        ObjectId::new("de4ac3cf96cb8a0893be22b03f5171d934f9d392"),
    );
636
637
638
639
    assert_eq!(repo_commit_detail.short_id, ObjectId::new("de4ac3cf"));
    assert_eq!(repo_commit_detail.title, "Merge topic 'mr-awards'");
    assert_eq!(repo_commit_detail.author_name, "Brad King");
    assert_eq!(repo_commit_detail.author_email, "brad.king@kitware.com");
640
641
    assert_eq!(repo_commit_detail.committer_name, "Kitware Robot");
    assert_eq!(repo_commit_detail.committer_email, "kwrobot@kitware.com");
Ben Boeckel's avatar
Ben Boeckel committed
642
643
    assert_eq!(
        repo_commit_detail.created_at,
644
        datetime((2016, 11, 8), (14, 30, 13, 0)),
Ben Boeckel's avatar
Ben Boeckel committed
645
646
647
648
649
650
651
    );
    assert_eq!(
        repo_commit_detail.message,
        "Merge topic 'mr-awards'\n\na222c553 gitlab: add a method for MR award \
         queries\n\nAcked-by: Kitware Robot <kwrobot@kitware.com>\nReviewed-by: Brad King \
         <brad.king@kitware.com>\nMerge-request: !46\n",
    );
652
653
654
    itertools::assert_equal(
        &repo_commit_detail.parent_ids,
        &[
Ben Boeckel's avatar
Ben Boeckel committed
655
            ObjectId::new("559f5f4a2bfe1f48e9e95afa09c029deb655cf7d"),
Ben Boeckel's avatar
Ben Boeckel committed
656
            ObjectId::new("a222c5539569cda6999b8069f1e51a5202c30711"),
Ben Boeckel's avatar
Ben Boeckel committed
657
658
659
660
        ],
    );
    assert_eq!(
        repo_commit_detail.committed_date,
661
        datetime((2016, 11, 8), (14, 30, 13, 0)),
Ben Boeckel's avatar
Ben Boeckel committed
662
663
664
    );
    assert_eq!(
        repo_commit_detail.authored_date,
665
        datetime((2016, 11, 8), (14, 30, 13, 0)),
Ben Boeckel's avatar
Ben Boeckel committed
666
    );
667
668
669
670
671
672
    let stats = repo_commit_detail.stats.as_ref().unwrap();
    assert_eq!(stats.additions, 8);
    assert_eq!(stats.deletions, 0);
    assert_eq!(stats.total, 8);
    let last_pipeline = repo_commit_detail.last_pipeline.as_ref().unwrap();
    assert_eq!(last_pipeline.id, PipelineId::new(34289));
673
    assert_eq!(last_pipeline.ref_.as_ref().unwrap(), "master");
674
675
676
677
678
679
    assert_eq!(
        last_pipeline.sha,
        ObjectId::new("de4ac3cf96cb8a0893be22b03f5171d934f9d392"),
    );
    assert_eq!(last_pipeline.status, StatusState::Success);
    assert_eq!(
680
681
        last_pipeline.created_at.unwrap(),
        datetime((2016, 11, 8), (14, 30, 16, 81)),
682
683
    );
    assert_eq!(
684
685
        last_pipeline.updated_at.unwrap(),
        datetime((2016, 11, 8), (14, 35, 32, 670)),
686
687
688
689
690
    );
    assert_eq!(
        last_pipeline.web_url,
        "https://gitlab.kitware.com/utils/rust-gitlab/pipelines/34289",
    );
Brad King's avatar
Brad King committed
691
    assert_eq!(repo_commit_detail.project_id, ProjectId::new(855));
692
}
Ben Boeckel's avatar
Ben Boeckel committed
693
694
695
696
697

#[test]
fn test_read_user() {
    let user: User = read_test_file("user");

698
    check_user_kwrobot(&user.clone().into());
Ben Boeckel's avatar
Ben Boeckel committed
699
    assert_eq!(
700
701
        user.created_at.unwrap(),
        datetime((2015, 2, 26), (15, 58, 34, 670)),
Ben Boeckel's avatar
Ben Boeckel committed
702
    );
703
    assert_eq!(user.is_admin, None);
Brad King's avatar
Brad King committed
704
    assert_eq!(user.highest_role, Some(AccessLevel::Owner));
705
    assert_eq!(user.bio, Some(String::new()));
Brad King's avatar
Brad King committed
706
    assert_eq!(user.private_profile, Some(false));
Ben Boeckel's avatar
Ben Boeckel committed
707
    assert_eq!(user.location, None);
708
    assert_eq!(user.public_email, Some(String::new()));
Ben Boeckel's avatar
Ben Boeckel committed
709
710
711
712
    assert_eq!(user.skype, "");
    assert_eq!(user.linkedin, "");
    assert_eq!(user.twitter, "");
    assert_eq!(user.website_url, "");
713
714
715
716
    assert_eq!(user.organization, None);
}

#[test]
717
718
719
fn test_read_user_public() {
    let user_public: UserPublic = read_test_file("user_public");

720
    check_user_kwrobot(&user_public.clone().into());
Ben Boeckel's avatar
Ben Boeckel committed
721
    assert_eq!(
722
723
        user_public.created_at.unwrap(),
        datetime((2015, 2, 26), (15, 58, 34, 670)),
Ben Boeckel's avatar
Ben Boeckel committed
724
    );
Brad King's avatar
Brad King committed
725
    assert_eq!(user_public.is_admin, Some(true));
726
    assert_eq!(user_public.bio, Some(String::new()));
Brad King's avatar
Brad King committed
727
    assert_eq!(user_public.private_profile, Some(false));
728
    assert_eq!(user_public.location, None);
729
    assert_eq!(user_public.public_email, Some(String::new()));
730
731
732
733
734
    assert_eq!(user_public.skype, "");
    assert_eq!(user_public.linkedin, "");
    assert_eq!(user_public.twitter, "");
    assert_eq!(user_public.website_url, "");
    assert_eq!(user_public.organization, None);
Ben Boeckel's avatar
Ben Boeckel committed
735
    assert_eq!(
736
737
        user_public.last_sign_in_at.unwrap(),
        datetime((2018, 10, 8), (17, 25, 29, 86)),
Ben Boeckel's avatar
Ben Boeckel committed
738
739
    );
    assert_eq!(
740
741
        user_public.last_activity_on.unwrap(),
        NaiveDate::from_ymd(2018, 10, 25),
Ben Boeckel's avatar
Ben Boeckel committed
742
743
    );
    assert_eq!(
744
745
        user_public.confirmed_at.unwrap(),
        datetime((2015, 2, 26), (15, 58, 34, 660)),
Ben Boeckel's avatar
Ben Boeckel committed
746
    );
Brad King's avatar
Brad King committed
747
    assert_eq!(user_public.email, "kwrobot@kitware.com");
Brad King's avatar
Brad King committed
748
    assert_eq!(user_public.theme_id, None);
Brad King's avatar
Brad King committed
749
    assert_eq!(user_public.color_scheme_id, ColorSchemeId::new(4));
750
    assert_eq!(user_public.projects_limit, 50);
Ben Boeckel's avatar
Ben Boeckel committed
751
    assert_eq!(
752
753
        user_public.current_sign_in_at.unwrap(),
        datetime((2018, 10, 11), (12, 36, 9, 687)),
Ben Boeckel's avatar
Ben Boeckel committed
754
    );
755
756
757
758
759
    assert!(user_public.identities.is_empty());
    assert_eq!(user_public.can_create_group, true);
    assert_eq!(user_public.can_create_project, true);
    assert_eq!(user_public.two_factor_enabled, true);
    assert_eq!(user_public.external, false);
Ben Boeckel's avatar
Ben Boeckel committed
760
}
Andrew Chin's avatar
Andrew Chin committed
761
762

#[test]
763
fn test_read_resource_label_events() {
Andrew Chin's avatar
Andrew Chin committed
764
765
766
    let event: ResourceLabelEvent = read_test_file("resource_label_event");

    assert_eq!(event.id, LabelEventId::new(10945));
767
    check_user_brad_king(&event.user);
768
769
770
771
772

    match &event.event_target() {
        Some(ResourceLabelEventTarget::Issue(id)) if id.value() == 69328 => {
            // this is the expected value
        },
Ben Boeckel's avatar
Ben Boeckel committed
773
        x => panic!("Unexpected resource_target: {:?}", x),
774
775
    }

Andrew Chin's avatar
Andrew Chin committed
776
777
778
779
    let label = event.label.unwrap();
    assert_eq!(label.id, LabelId::new(1720));
    assert_eq!(label.name, "area:doc");
    assert_eq!(label.color, LabelColor::from_rgb(0x58, 0x43, 0xAD));
780
    assert_eq!(label.description.as_ref().unwrap(), "Documentation issues");
Andrew Chin's avatar
Andrew Chin committed
781
}
782
783
784
785
786

#[test]
fn test_read_pipelines() {
    let pipeline_basic: PipelineBasic = read_test_file("pipeline_basic");

Brad King's avatar
Brad King committed
787
    assert_eq!(pipeline_basic.id, PipelineId::new(149_249));
Ben Boeckel's avatar
Ben Boeckel committed
788
    assert_eq!(pipeline_basic.status, StatusState::Success);
789
    assert_eq!(pipeline_basic.ref_.as_ref().unwrap(), "master");
Ben Boeckel's avatar
Ben Boeckel committed
790
791
    assert_eq!(
        pipeline_basic.sha,
Brad King's avatar
Brad King committed
792
793
794
        ObjectId::new("f6ebd73d4a8f42f6a642030c27b66330023b7a88"),
    );
    assert_eq!(
795
796
        pipeline_basic.created_at.unwrap(),
        datetime((2019, 10, 25), (19, 22, 38, 463)),
Brad King's avatar
Brad King committed
797
798
    );
    assert_eq!(
799
800
        pipeline_basic.updated_at.unwrap(),
        datetime((2019, 10, 25), (19, 35, 24, 570)),
Ben Boeckel's avatar
Ben Boeckel committed
801
802
803
    );
    assert_eq!(
        pipeline_basic.web_url,
Brad King's avatar
Brad King committed
804
        "https://gitlab.kitware.com/utils/rust-gitlab/pipelines/149249",
Ben Boeckel's avatar
Ben Boeckel committed
805
    );
806
807
808
809
810
811
}

#[test]
fn test_read_pipeline() {
    let pipeline: Pipeline = read_test_file("pipeline");

812
    assert_eq!(pipeline.id, PipelineId::new(145_400));
813
    assert_eq!(pipeline.status, StatusState::Success);
814
    assert_eq!(pipeline.ref_.as_ref().unwrap(), "master");
Ben Boeckel's avatar
Ben Boeckel committed
815
816
    assert_eq!(
        pipeline.sha,
817
        ObjectId::new("7134adce4522c399cdab16e128b0a1af15b93f14"),
Ben Boeckel's avatar
Ben Boeckel committed
818
    );
Brad King's avatar
Brad King committed
819
820
821
822
    assert_eq!(
        pipeline.before_sha,
        Some(ObjectId::new("0000000000000000000000000000000000000000"))
    );
823
824
    assert_eq!(pipeline.tag, false);
    assert_eq!(pipeline.yaml_errors, None);
Ben Boeckel's avatar
Ben Boeckel committed
825
    assert_eq!(
826
827
        pipeline.created_at.unwrap(),
        datetime((2019, 9, 3), (18, 9, 47, 178)),
Ben Boeckel's avatar
Ben Boeckel committed
828
829
    );
    assert_eq!(
830
831
        pipeline.updated_at.unwrap(),
        datetime((2019, 9, 3), (18, 15, 47, 18)),
Ben Boeckel's avatar
Ben Boeckel committed
832
833
    );
    assert_eq!(
834
835
        pipeline.started_at.unwrap(),
        datetime((2019, 9, 3), (18, 9, 51, 465)),
Ben Boeckel's avatar
Ben Boeckel committed
836
837
    );
    assert_eq!(
838
839
        pipeline.finished_at.unwrap(),
        datetime((2019, 9, 3), (18, 15, 47, 13)),
Ben Boeckel's avatar
Ben Boeckel committed
840
    );
841
842
843
    assert_eq!(pipeline.committed_at, None);
    assert_eq!(pipeline.duration, Some(0));
    assert_eq!(pipeline.coverage, None);
Ben Boeckel's avatar
Ben Boeckel committed
844
845
846
847
    assert_eq!(
        pipeline.web_url,
        "https://gitlab.kitware.com/utils/rust-gitlab/pipelines/145400",
    );
848
849

    // nested user
850
    check_user_buildbot(&pipeline.user);
851
852
853
854
855
856
857
858
859
860
861
862
863

    // nested detailed status
    assert_eq!(
        pipeline.detailed_status,
        json!({
            "details_path": "/utils/rust-gitlab/pipelines/145400",
            "favicon": "/assets/ci_favicons/favicon_status_success-8451333011eee8ce9f2ab25dc487fe24a8758c694827a582f17f42b0a90446a2.png",
            "group": "success",
            "has_details": true,
            "icon": "status_success",
            "illustration": null,
            "label": "passed",
            "text": "passed",
Ben Boeckel's avatar
Ben Boeckel committed
864
            "tooltip": "passed",
865
866
867
868
869
870
871
872
873
874
875
876
        }),
    );
}

#[test]
fn test_read_pipeline_variables() {
    let var: PipelineVariable = read_test_file("pipeline_variable");

    assert_eq!(var.key, "RUN_NIGHTLY_BUILD");
    assert_eq!(var.variable_type, PipelineVariableType::EnvVar);
    assert_eq!(var.value, "true");
}
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895

#[test]
fn test_read_group() {
    let group: Group = read_test_file("group");

    assert_eq!(group.id, GroupId::new(498));
    assert_eq!(group.name, "Utils");
    assert_eq!(group.path, "utils");
    assert_eq!(group.description.as_ref().unwrap(), "");
    assert_eq!(group.visibility, VisibilityLevel::Public);
    assert_eq!(group.lfs_enabled, true);
    assert_eq!(group.avatar_url, None);
    assert_eq!(group.web_url, "https://gitlab.kitware.com/groups/utils");
    assert_eq!(group.request_access_enabled, false);
    assert_eq!(group.full_name, "Utils");
    assert_eq!(group.full_path, "utils");
    assert_eq!(group.parent_id, None);
    assert!(group.statistics.is_none());
}