blob: 8700a99292c41069681d59eae3f9ccb172a63e24 [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001// Copyright 2020 The Chromium Authors
Copybara854996b2021-09-07 19:36:02 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import {assert} from 'chai';
6import {ResourceNameError, pathsToFieldMask, extractUserId,
7 extractProjectDisplayName, extractProjectFromProjectMember,
8 projectAndUserToStarName, projectMemberToProjectName} from './converters.js';
9
10describe('pathsToFieldMask', () => {
11 it('converts an array of strings to a FieldMask', () => {
12 assert.equal(pathsToFieldMask(['foo', 'barQux', 'qaz']), 'foo,barQux,qaz');
13 });
14});
15
16describe('extractUserId', () => {
17 it('throws error on improperly formatted resource name', () => {
18 assert.throws(() => extractUserId('projects/1234'),
19 ResourceNameError);
20 assert.throws(() => extractUserId('users/notAnId'),
21 ResourceNameError);
22 assert.throws(() => extractUserId('user/1234'),
23 ResourceNameError);
24 });
25
26 it('extracts user ID', () => {
27 assert.equal(extractUserId('users/1234'), '1234');
28 });
29});
30
31describe('extractProjectDisplayName', () => {
32 it('throws error on improperly formatted resource name', () => {
33 assert.throws(() => extractProjectDisplayName('users/1234'),
34 ResourceNameError);
35 assert.throws(() => extractProjectDisplayName('projects/(what)'),
36 ResourceNameError);
37 assert.throws(() => extractProjectDisplayName('project/test'),
38 ResourceNameError);
39 assert.throws(() => extractProjectDisplayName('projects/-test-'),
40 ResourceNameError);
41 });
42
43 it('extracts project display name', () => {
44 assert.equal(extractProjectDisplayName('projects/1234'), '1234');
45 assert.equal(extractProjectDisplayName('projects/monorail'), 'monorail');
46 assert.equal(extractProjectDisplayName('projects/test-project'),
47 'test-project');
48 assert.equal(extractProjectDisplayName('projects/what-is-love2'),
49 'what-is-love2');
50 });
51});
52
53describe('extractProjectFromProjectMember', () => {
54 it('throws error on improperly formatted resource name', () => {
55 assert.throws(
56 () => extractProjectFromProjectMember(
57 'projects/monorail/members/fakeName'),
58 ResourceNameError);
59 assert.throws(
60 () => extractProjectFromProjectMember(
61 'projects/-invalid-project-/members/1234'),
62 ResourceNameError);
63 assert.throws(
64 () => extractProjectFromProjectMember(
65 'projects/monorail/member/1234'),
66 ResourceNameError);
67 });
68
69 it('extracts project display name', () => {
70 assert.equal(extractProjectFromProjectMember(
71 'projects/1234/members/1234'), '1234');
72 assert.equal(extractProjectFromProjectMember(
73 'projects/monorail/members/1234'), 'monorail');
74 assert.equal(extractProjectFromProjectMember(
75 'projects/test-project/members/1234'), 'test-project');
76 assert.equal(extractProjectFromProjectMember(
77 'projects/what-is-love2/members/1234'), 'what-is-love2');
78 });
79});
80
81describe('projectAndUserToStarName', () => {
82 it('throws error on improperly formatted resource name', () => {
83 assert.throws(
84 () => projectAndUserToStarName('users/1234', 'projects/monorail'),
85 ResourceNameError);
86 });
87
88 it('generates project star resource name', () => {
89 assert.equal(projectAndUserToStarName('projects/monorail', 'users/1234'),
90 'users/1234/projectStars/monorail');
91 });
92});
93
94describe('projectMemberToProjectName', () => {
95 it('throws error on improperly formatted resource name', () => {
96 assert.throws(
97 () => projectMemberToProjectName(
98 'projects/monorail/members/fakeName'),
99 ResourceNameError);
100 });
101
102 it('creates project resource name', () => {
103 assert.equal(projectMemberToProjectName(
104 'projects/1234/members/1234'), 'projects/1234');
105 assert.equal(projectMemberToProjectName(
106 'projects/monorail/members/1234'), 'projects/monorail');
107 assert.equal(projectMemberToProjectName(
108 'projects/test-project/members/1234'), 'projects/test-project');
109 assert.equal(projectMemberToProjectName(
110 'projects/what-is-love2/members/1234'), 'projects/what-is-love2');
111 });
112});