types.rs 30.6 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;

Brad King's avatar
Brad King committed
9
use crates::chrono::{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
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
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!(
        user.avatar_url,
        Some(
            "https://secure.gravatar.com/avatar/0617392a2f9fd505720d0c42cefc1a10?s=80&d=identicon"
                .into(),
        ),
    );
    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!(
        user.avatar_url,
        Some(
            "https://secure.gravatar.com/avatar/2f5f7e99190174edb5a2f66b8653b0b2?s=80&d=identicon"
                .into(),
        ),
    );
    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!(
        user.avatar_url,
        Some(
            "https://secure.gravatar.com/avatar/9ddcd45fcb89d966aab95b1f1002f84c?s=80&d=identicon"
                .into(),
        ),
    );
    assert_eq!(user.id, UserId::new(11));
}

fn check_user_buildbot(user: &UserBasic) {
    assert_eq!(
        user.avatar_url,
        Some("https://gitlab.kitware.com/uploads/-/system/user/avatar/35/buildbot-logo.png".into()),
    );
    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);
}

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

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

#[test]
102
103
fn test_read_award_emoji() {
    let award_emoji: AwardEmoji = read_test_file("award_emoji");
104

105
106
    assert_eq!(award_emoji.id, AwardId::new(335));
    assert_eq!(award_emoji.name, "8ball");
107
    check_user_ben_boeckel(&award_emoji.user);
Ben Boeckel's avatar
Ben Boeckel committed
108
109
110
111
112
113
114
115
116
117
    assert_eq!(
        award_emoji.created_at,
        Utc.ymd(2016, 12, 7).and_hms_milli(16, 23, 46, 742),
    );
    assert_eq!(
        award_emoji.updated_at,
        Utc.ymd(2016, 12, 7).and_hms_milli(16, 23, 46, 742),
    );
    assert_eq!(
        award_emoji.awardable_id(),
118
        AwardableId::Note(NoteId::new(177_359)),
Ben Boeckel's avatar
Ben Boeckel committed
119
    );
120
121
122
123
124
125
126
127
128
129
130
    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);
131
    check_user_ben_boeckel(&commit_note.author);
Ben Boeckel's avatar
Ben Boeckel committed
132
133
134
135
    assert_eq!(
        commit_note.created_at,
        Utc.ymd(2016, 12, 7).and_hms_milli(16, 28, 33, 966),
    );
136
137
138
139
140
141
}

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

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

#[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
176
    assert_eq!(issue.iid, IssueInternalId::new(6));
177
178
    assert_eq!(issue.project_id, ProjectId::new(855));
    assert_eq!(issue.title, "fix documentation warnings");
179
    assert_eq!(issue.description, Some(String::new()));
180
    assert_eq!(issue.state, IssueState::Closed);
Ben Boeckel's avatar
Ben Boeckel committed
181
182
183
184
185
186
    assert_eq!(
        issue.created_at,
        Utc.ymd(2016, 10, 30).and_hms_milli(18, 54, 28, 954),
    );
    assert_eq!(
        issue.updated_at,
Brad King's avatar
Brad King committed
187
        Utc.ymd(2019, 7, 17).and_hms_milli(13, 53, 48, 869),
Ben Boeckel's avatar
Ben Boeckel committed
188
    );
Brad King's avatar
Brad King committed
189
    assert_eq!(issue.closed_at, None);
Brad King's avatar
Brad King committed
190
    assert!(issue.closed_by.is_none());
191
    itertools::assert_equal(&issue.labels, &["area:doc"]);
192
    assert!(issue.milestone.is_none());
193
    check_user_ben_boeckel(&issue.author);
194
195
196
197
198
199
    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
200
    assert_eq!(issue.subscribed, Some(true));
201
    assert_eq!(issue.user_notes_count, 0);
Brad King's avatar
Brad King committed
202
    assert_eq!(issue.merge_requests_count, 1);
203
204
205
    assert_eq!(issue.upvotes, 0);
    assert_eq!(issue.downvotes, 0);
    assert_eq!(issue.due_date, None);
Brad King's avatar
Brad King committed
206
    assert_eq!(issue.has_tasks, Some(false));
207
    assert_eq!(issue.confidential, false);
Brad King's avatar
Brad King committed
208
    assert_eq!(issue.discussion_locked, None);
Ben Boeckel's avatar
Ben Boeckel committed
209
210
211
212
    assert_eq!(
        issue.web_url,
        "https://gitlab.kitware.com/utils/rust-gitlab/issues/6",
    );
Brad King's avatar
Brad King committed
213
    assert!(issue.has_links());
214
215
216
217
218
219
}

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

220
221
    let issue = if let IssueReference::Internal(issue) = issue_reference {
        issue
222
223
    } else {
        panic!("expected to have an internal issue reference");
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
    };

    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!(
        issue.description,
        Some(
            "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"
                .into()
        ),
    );
    assert_eq!(issue.state, IssueState::Closed);
    assert_eq!(
        issue.created_at,
        Utc.ymd(2016, 10, 4).and_hms_milli(18, 59, 37, 178),
    );
    assert_eq!(
        issue.updated_at,
        Utc.ymd(2017, 7, 7).and_hms_milli(6, 31, 5, 370),
    );
    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",
    );
267
268
269
270
271
272
273
274
275
276
}

#[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
277
278
279
280
    assert_eq!(
        member.avatar_url,
        Some(
            "https://secure.gravatar.com/avatar/9ddcd45fcb89d966aab95b1f1002f84c?s=80&d=identicon"
281
                .into()
Ben Boeckel's avatar
Ben Boeckel committed
282
283
        ),
    );
284
285
286
287
288
289
290
291
292
    assert_eq!(member.web_url, "https://gitlab.kitware.com/kwrobot");
    assert_eq!(member.access_level, 50);
    assert_eq!(member.expires_at, None);
}

#[test]
fn test_read_merge_request() {
    let merge_request: MergeRequest = read_test_file("merge_request");

293
    assert_eq!(merge_request.id, MergeRequestId::new(20215));
Brad King's avatar
Brad King committed
294
    assert_eq!(merge_request.iid, MergeRequestInternalId::new(35));
295
    assert_eq!(merge_request.project_id, ProjectId::new(855));
296
    assert_eq!(merge_request.title, "gitlab: expose hook addition API");
297
    assert_eq!(merge_request.description, Some("Fixes #5.".into()));
298
    assert_eq!(merge_request.state, MergeRequestState::Merged);
Ben Boeckel's avatar
Ben Boeckel committed
299
300
301
302
303
304
305
306
307
308
309
310
    assert_eq!(
        merge_request.created_at,
        Utc.ymd(2016, 10, 4).and_hms_milli(19, 56, 43, 276),
    );
    assert_eq!(
        merge_request.updated_at,
        Utc.ymd(2016, 10, 4).and_hms_milli(20, 18, 57, 940),
    );
    assert_eq!(
        merge_request.merged_at,
        Some(Utc.ymd(2016, 10, 4).and_hms_milli(20, 18, 57, 914)),
    );
Brad King's avatar
Brad King committed
311
312
    assert!(merge_request.merged_by.is_none());
    assert!(merge_request.closed_by.is_none());
313
    assert_eq!(merge_request.target_branch, "master");
314
    assert_eq!(merge_request.source_branch, "add_hook-api");
315
316
    assert_eq!(merge_request.upvotes, 0);
    assert_eq!(merge_request.downvotes, 0);
317
    check_user_ben_boeckel(&merge_request.author);
318
319
320
321
322
323
    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);
324
    assert_eq!(merge_request.source_project_id, ProjectId::new(856));
325
326
327
    assert_eq!(merge_request.target_project_id, ProjectId::new(855));
    assert!(merge_request.labels.is_empty());
    assert_eq!(merge_request.work_in_progress, false);
328
329
    assert_eq!(merge_request.allow_collaboration, None);
    assert_eq!(merge_request.allow_maintainer_to_push, None);
330
    assert!(merge_request.milestone.is_none());
Brad King's avatar
Brad King committed
331
    assert_eq!(merge_request.squash, false);
Brad King's avatar
Brad King committed
332
    assert_eq!(merge_request.merge_when_pipeline_succeeds, false);
333
    assert_eq!(merge_request.merge_status, MergeStatus::CanBeMerged);
Ben Boeckel's avatar
Ben Boeckel committed
334
335
336
337
    assert_eq!(
        merge_request.sha,
        Some(ObjectId::new("04e94ae667024a62a90179f395bfdc2b35f3efd2")),
    );
338
    let diff_refs = merge_request.diff_refs.as_ref().unwrap();
Ben Boeckel's avatar
Ben Boeckel committed
339
    assert_eq!(
340
        diff_refs.base_sha,
Ben Boeckel's avatar
Ben Boeckel committed
341
342
343
        Some(ObjectId::new("981262b03fc0149c1677ca51ea47b570e30d6a90")),
    );
    assert_eq!(
344
        diff_refs.head_sha,
Ben Boeckel's avatar
Ben Boeckel committed
345
346
347
        Some(ObjectId::new("04e94ae667024a62a90179f395bfdc2b35f3efd2")),
    );
    assert_eq!(
348
        diff_refs.start_sha,
Ben Boeckel's avatar
Ben Boeckel committed
349
350
        Some(ObjectId::new("981262b03fc0149c1677ca51ea47b570e30d6a90")),
    );
Brad King's avatar
Brad King committed
351
352
    assert_eq!(merge_request.merge_error, None);
    assert_eq!(merge_request.rebase_in_progress, None);
353
    assert_eq!(merge_request.merge_commit_sha, None);
Brad King's avatar
Brad King committed
354
    assert_eq!(merge_request.subscribed, Some(true));
355
    check_empty_time_stats(&merge_request.time_stats);
356
    assert_eq!(merge_request.changes_count, Some("3".into()));
357
    assert_eq!(merge_request.user_notes_count, 3);
Brad King's avatar
Brad King committed
358
    assert_eq!(merge_request.discussion_locked, None);
359
360
    assert_eq!(merge_request.should_remove_source_branch, None);
    assert_eq!(merge_request.force_remove_source_branch, Some(true));
361
    assert_eq!(merge_request.user.unwrap().can_merge, true);
Ben Boeckel's avatar
Ben Boeckel committed
362
363
364
365
    assert_eq!(
        merge_request.web_url,
        "https://gitlab.kitware.com/utils/rust-gitlab/merge_requests/35",
    );
366
367
368
369
370
371
}

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

372
    assert_eq!(note.id, NoteId::new(177_373));
Ben Boeckel's avatar
Ben Boeckel committed
373
    assert_eq!(note.body, "Status changed to merged");
374
    assert_eq!(note.attachment, None);
375
    check_user_kwrobot(&note.author);
Ben Boeckel's avatar
Ben Boeckel committed
376
377
378
379
380
381
382
383
    assert_eq!(
        note.created_at,
        Utc.ymd(2016, 10, 4).and_hms_milli(20, 18, 57, 937),
    );
    assert_eq!(
        note.updated_at,
        Utc.ymd(2016, 10, 4).and_hms_milli(20, 18, 57, 937),
    );
Brad King's avatar
Brad King committed
384
385
386
    assert_eq!(note.resolvable, false);
    assert_eq!(note.resolved, None);
    assert!(note.resolved_by.is_none());
387
    assert_eq!(note.system, true);
Ben Boeckel's avatar
Ben Boeckel committed
388
389
390
391
392
393
394
395
396
397
    assert_eq!(
        note.noteable_id(),
        Some(NoteableId::MergeRequest(MergeRequestId::new(20215))),
    );
    assert_eq!(
        note.noteable_iid(),
        Some(NoteableInternalId::MergeRequest(
            MergeRequestInternalId::new(35)
        )),
    );
398
399
400
    assert_eq!(note.noteable_type, NoteType::MergeRequest);
}

401
402
403
#[test]
fn test_read_singlenote_discussion() {
    let discussions: Vec<Discussion> = read_test_file("discussion");
Ben Boeckel's avatar
Ben Boeckel committed
404
405
406
407
    let discussion = discussions
        .iter()
        .find(|x| x.id.value() == "18ea341cb10e952889e277836ba638c6b17ff26c")
        .unwrap();
408
409
410
    assert!(discussion.individual_note);
    assert_eq!(discussion.notes.len(), 1);
    let note = discussion.notes.get(0).unwrap();
411
    assert!(!note.resolvable);
412
    assert!(note.position.is_none());
413
    assert_eq!(note.note_type, None)
414
415
416
417
418
}

#[test]
fn test_read_nocode_discussion() {
    let discussions: Vec<Discussion> = read_test_file("discussion");
Ben Boeckel's avatar
Ben Boeckel committed
419
420
421
422
    let discussion = discussions
        .iter()
        .find(|x| x.id.value() == "a4d5505b3556eaa45edbe567af7aebc1760dedd7")
        .unwrap();
423
    assert!(!discussion.individual_note);
424
425
426
427
428
429
    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);

430
    assert_eq!(question.resolved, Some(true));
431
432
433
434

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

435
436
    assert_eq!(question.id, NoteId::new(607_911));
    assert_eq!(comment.id, NoteId::new(607_912));
437
438
439
440
441
442
443
444

    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
445
446
447
448
    let discussion = discussions
        .into_iter()
        .find(|x| x.id.value() == "9f4998b2308728b95cff52af97019479e1269183")
        .unwrap();
449
    assert!(!discussion.individual_note);
450
451
    let note = discussion.notes.get(0).unwrap();
    assert!(note.resolvable);
452
    assert_eq!(note.resolved, Some(true));
453
    check_user_brad_king(&note.author);
454
    assert_eq!(note.id, NoteId::new(619_272));
455
    assert_eq!(note.note_type, Some(DiscussionNoteType::DiffNote));
456
457
458
459
460
461
462
463
    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");
464
465
}

466
467
468
469
470
#[test]
fn test_read_project() {
    let project: Project = read_test_file("project");

    assert_eq!(project.id, ProjectId::new(855));
Ben Boeckel's avatar
Ben Boeckel committed
471
472
    assert_eq!(
        project.description,
473
        Some("Rust library for communicating with a Gitlab instance.".into()),
Ben Boeckel's avatar
Ben Boeckel committed
474
    );
475
    assert_eq!(project.default_branch, Some("master".into()));
476
477
    assert!(project.tag_list.is_empty());
    assert_eq!(project.archived, false);
Brad King's avatar
Brad King committed
478
    assert_eq!(project.empty_repo, false);
Brad King's avatar
Brad King committed
479
    assert_eq!(project.visibility, VisibilityLevel::Public);
Ben Boeckel's avatar
Ben Boeckel committed
480
481
482
483
484
485
486
487
488
489
490
491
492
493
    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!(
        project.readme_url,
494
        Some("https://gitlab.kitware.com/utils/rust-gitlab/blob/master/README.md".into()),
Ben Boeckel's avatar
Ben Boeckel committed
495
    );
496
497
498
499
500
501
    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
502
503
504
505
506
507
    assert_eq!(
        project.created_at,
        Utc.ymd(2016, 6, 29).and_hms_milli(17, 35, 12, 495),
    );
    assert_eq!(
        project.last_activity_at,
Brad King's avatar
Brad King committed
508
        Utc.ymd(2019, 7, 30).and_hms_milli(16, 42, 57, 649),
Ben Boeckel's avatar
Ben Boeckel committed
509
    );
Brad King's avatar
Brad King committed
510
    assert_eq!(project.import_error, None);
511
512
513
    assert_eq!(project.shared_runners_enabled, true);
    assert_eq!(project.lfs_enabled, true);
    assert_eq!(project.creator_id, UserId::new(13));
514
    check_namespace_utils(&project.namespace);
515
516
    assert!(project.forked_from_project.is_none());
    assert_eq!(project.avatar_url, None);
Brad King's avatar
Brad King committed
517
    assert_eq!(project.ci_config_path, None);
Brad King's avatar
Brad King committed
518
519
520
    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
521
    assert_eq!(project.public_jobs, true);
522
    assert!(project.shared_with_groups.is_empty());
Brad King's avatar
Brad King committed
523
    assert_eq!(project.only_allow_merge_if_pipeline_succeeds, Some(false));
Ben Boeckel's avatar
Ben Boeckel committed
524
525
526
527
    assert_eq!(
        project.only_allow_merge_if_all_discussions_are_resolved,
        None,
    );
Brad King's avatar
Brad King committed
528
    assert_eq!(project.printing_merge_request_link_enabled, Some(true));
Brad King's avatar
Brad King committed
529
    assert_eq!(project.request_access_enabled, false);
Brad King's avatar
Brad King committed
530
    assert_eq!(project.resolve_outdated_diff_discussions, None);
531
532

    assert_eq!(project.jobs_enabled, false);
533
534
535
536
    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);
537

Ben Boeckel's avatar
Ben Boeckel committed
538
539
540
541
    assert_eq!(
        project.builds_access_level,
        FeatureVisibilityLevel::Disabled,
    );
Brad King's avatar
Brad King committed
542
    assert_eq!(project.issues_access_level, FeatureVisibilityLevel::Enabled);
Ben Boeckel's avatar
Ben Boeckel committed
543
544
545
546
547
548
549
550
551
552
553
554
    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
555
556
    assert_eq!(project.wiki_access_level, FeatureVisibilityLevel::Enabled);

557
    assert_eq!(project.merge_method, Some("merge".into()));
558
559
560
561
562
563
    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());
564
565
566
567
568
569
}

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

570
571
    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
572
573
574
575
    assert_eq!(
        project_hook.created_at,
        Utc.ymd(2016, 12, 16).and_hms_milli(16, 37, 24, 589),
    );
576
    assert_eq!(project_hook.push_events, true);
577
    assert_eq!(project_hook.push_events_branch_filter, None);
578
579
    assert_eq!(project_hook.tag_push_events, true);
    assert_eq!(project_hook.issues_events, true);
580
    assert_eq!(project_hook.confidential_issues_events, Some(true));
581
582
    assert_eq!(project_hook.merge_requests_events, true);
    assert_eq!(project_hook.note_events, true);
583
    assert_eq!(project_hook.confidential_note_events, Some(true));
Brad King's avatar
Brad King committed
584
    assert_eq!(project_hook.repository_update_events, false);
585
    assert_eq!(project_hook.enable_ssl_verification, true);
Brad King's avatar
Brad King committed
586
    assert_eq!(project_hook.job_events, true);
587
588
    assert_eq!(project_hook.pipeline_events, true);
    assert_eq!(project_hook.wiki_page_events, true);
589
590
591
592
593
594
595
}

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

    assert_eq!(repo_branch.name, "master");
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
    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,
        Utc.ymd(2018, 7, 12).and_hms_milli(12, 50, 24, 0),
    );
    assert_eq!(
        commit.committed_date,
        Utc.ymd(2018, 7, 12).and_hms_milli(12, 50, 24, 0),
    );
    assert_eq!(
        commit.created_at,
        Utc.ymd(2018, 7, 12).and_hms_milli(12, 50, 24, 0),
    );
    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");
620
621
622
    itertools::assert_equal(
        &commit.parent_ids,
        &[ObjectId::new("5c81cc05661dcbb5fd923cca093920816c21ef7e")],
623
    );
624
    assert_eq!(repo_branch.merged, Some(false));
625
626
627
    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
628
    assert_eq!(repo_branch.can_push, Some(true));
Brad King's avatar
Brad King committed
629
    assert_eq!(repo_branch.default, Some(true));
630
631
632
633
634
635
}

#[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
636
637
638
639
    assert_eq!(
        repo_commit_detail.id,
        ObjectId::new("de4ac3cf96cb8a0893be22b03f5171d934f9d392"),
    );
640
641
642
643
    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");
644
645
    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
646
647
648
649
650
651
652
653
654
655
    assert_eq!(
        repo_commit_detail.created_at,
        Utc.ymd(2016, 11, 8).and_hms_milli(14, 30, 13, 0),
    );
    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",
    );
656
657
658
    itertools::assert_equal(
        &repo_commit_detail.parent_ids,
        &[
Ben Boeckel's avatar
Ben Boeckel committed
659
            ObjectId::new("559f5f4a2bfe1f48e9e95afa09c029deb655cf7d"),
Ben Boeckel's avatar
Ben Boeckel committed
660
            ObjectId::new("a222c5539569cda6999b8069f1e51a5202c30711"),
Ben Boeckel's avatar
Ben Boeckel committed
661
662
663
664
665
666
667
668
669
670
        ],
    );
    assert_eq!(
        repo_commit_detail.committed_date,
        Utc.ymd(2016, 11, 8).and_hms_milli(14, 30, 13, 0),
    );
    assert_eq!(
        repo_commit_detail.authored_date,
        Utc.ymd(2016, 11, 8).and_hms_milli(14, 30, 13, 0),
    );
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
    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));
    assert_eq!(last_pipeline.ref_, Some("master".into()));
    assert_eq!(
        last_pipeline.sha,
        ObjectId::new("de4ac3cf96cb8a0893be22b03f5171d934f9d392"),
    );
    assert_eq!(last_pipeline.status, StatusState::Success);
    assert_eq!(
        last_pipeline.created_at,
        Some(Utc.ymd(2016, 11, 8).and_hms_milli(14, 30, 16, 81)),
    );
    assert_eq!(
        last_pipeline.updated_at,
        Some(Utc.ymd(2016, 11, 8).and_hms_milli(14, 35, 32, 670)),
    );
    assert_eq!(
        last_pipeline.web_url,
        "https://gitlab.kitware.com/utils/rust-gitlab/pipelines/34289",
    );
Brad King's avatar
Brad King committed
695
    assert_eq!(repo_commit_detail.project_id, ProjectId::new(855));
696
}
Ben Boeckel's avatar
Ben Boeckel committed
697
698
699
700
701

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

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

#[test]
721
722
723
fn test_read_user_public() {
    let user_public: UserPublic = read_test_file("user_public");

724
    check_user_kwrobot(&user_public.clone().into());
Ben Boeckel's avatar
Ben Boeckel committed
725
726
727
728
    assert_eq!(
        user_public.created_at,
        Some(Utc.ymd(2015, 2, 26).and_hms_milli(15, 58, 34, 670)),
    );
Brad King's avatar
Brad King committed
729
    assert_eq!(user_public.is_admin, Some(true));
730
    assert_eq!(user_public.bio, Some("".into()));
Brad King's avatar
Brad King committed
731
    assert_eq!(user_public.private_profile, Some(false));
732
    assert_eq!(user_public.location, None);
733
    assert_eq!(user_public.public_email, Some("".into()));
734
735
736
737
738
    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
739
740
741
742
743
744
745
746
747
748
749
750
    assert_eq!(
        user_public.last_sign_in_at,
        Some(Utc.ymd(2018, 10, 8).and_hms_milli(17, 25, 29, 86)),
    );
    assert_eq!(
        user_public.last_activity_on,
        Some(NaiveDate::from_ymd(2018, 10, 25)),
    );
    assert_eq!(
        user_public.confirmed_at,
        Some(Utc.ymd(2015, 2, 26).and_hms_milli(15, 58, 34, 660)),
    );
Brad King's avatar
Brad King committed
751
    assert_eq!(user_public.email, "kwrobot@kitware.com");
Brad King's avatar
Brad King committed
752
    assert_eq!(user_public.theme_id, None);
Brad King's avatar
Brad King committed
753
    assert_eq!(user_public.color_scheme_id, ColorSchemeId::new(4));
754
    assert_eq!(user_public.projects_limit, 50);
Ben Boeckel's avatar
Ben Boeckel committed
755
756
757
758
    assert_eq!(
        user_public.current_sign_in_at,
        Some(Utc.ymd(2018, 10, 11).and_hms_milli(12, 36, 9, 687)),
    );
759
760
761
762
763
    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
764
}
Andrew Chin's avatar
Andrew Chin committed
765
766

#[test]
767
fn test_read_resource_label_events() {
Andrew Chin's avatar
Andrew Chin committed
768
769
770
    let event: ResourceLabelEvent = read_test_file("resource_label_event");

    assert_eq!(event.id, LabelEventId::new(10945));
771
    check_user_brad_king(&event.user);
772
773
774
775
776

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

Andrew Chin's avatar
Andrew Chin committed
780
781
782
783
    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));
784
    assert_eq!(label.description, Some("Documentation issues".into()));
Andrew Chin's avatar
Andrew Chin committed
785
}
786
787
788
789
790

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

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

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

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

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

    // 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
865
            "tooltip": "passed",
866
867
868
869
870
871
872
873
874
875
876
877
        }),
    );
}

#[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");
}