forked from all-contributors/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitlab.js
136 lines (118 loc) · 3.88 KB
/
gitlab.js
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
import nock from 'nock'
import gitlabAPI from '../gitlab'
const getUserInfo = gitlabAPI.getUserInfo
async function rejects(promise) {
const error = await promise.catch(e => e)
expect(error).toBeTruthy()
}
test('handle errors', async () => {
nock('https://gitlab.com')
.get('/api/v4/users?username=nodisplayname')
.reply(200, [])
await rejects(getUserInfo('nodisplayname'))
})
test('fill in the name when it is returned', async () => {
nock('https://gitlab.com')
.get('/api/v4/users?username=nodisplayname')
.reply(200, [
{
username: 'nodisplayname',
name: 'No Display Name',
avatar_url:
'http://www.gravatar.com/avatar/3186450a99d1641bf75a44baa23f0826?s=80\u0026d=identicon',
web_url: 'https://gitlab.com/nodisplayname',
},
])
const info = await getUserInfo('nodisplayname')
expect(info.name).toBe('No Display Name')
})
test('fill in the name when null is returned', async () => {
nock('https://gitlab.com')
.get('/api/v4/users?username=nodisplayname')
.reply(200, [
{
username: 'nodisplayname',
name: null,
avatar_url:
'http://www.gravatar.com/avatar/3186450a99d1641bf75a44baa23f0826?s=80\u0026d=identicon',
web_url: 'https://gitlab.com/nodisplayname',
},
])
const info = await getUserInfo('nodisplayname')
expect(info.name).toBe('nodisplayname')
})
test('fill in the name when an empty string is returned', async () => {
nock('https://gitlab.com')
.get('/api/v4/users?username=nodisplayname')
.reply(200, [
{
username: 'nodisplayname',
name: '',
avatar_url:
'http://www.gravatar.com/avatar/3186450a99d1641bf75a44baa23f0826?s=80\u0026d=identicon',
web_url: 'https://gitlab.com/nodisplayname',
},
])
const info = await getUserInfo('nodisplayname')
expect(info.name).toBe('nodisplayname')
})
test('append http when no absolute link is provided', async () => {
nock('https://gitlab.com')
.get('/api/v4/users?username=nodisplayname')
.reply(200, [
{
username: 'nodisplayname',
name: 'No Display Name',
avatar_url:
'http://www.gravatar.com/avatar/3186450a99d1641bf75a44baa23f0826?s=80\u0026d=identicon',
web_url: 'www.gitlab.com/nodisplayname',
},
])
const info = await getUserInfo('nodisplayname')
expect(info.profile).toBe('http://www.gitlab.com/nodisplayname')
})
test('retrieve user from a different gitlab registry', async () => {
nock('http://gitlab.myhost.com:3000')
.get('/api/v4/users?username=nodisplayname')
.reply(200, [
{
username: 'nodisplayname',
name: 'No Display Name',
avatar_url:
'http://www.gravatar.com/avatar/3186450a99d1641bf75a44baa23f0826?s=80\u0026d=identicon',
web_url: 'https://gitlab.com/nodisplayname',
},
])
const info = await getUserInfo(
'nodisplayname',
'http://gitlab.myhost.com:3000',
)
expect(info.name).toBe('No Display Name')
})
test('retrieve user from a gitlab registry that needs a token', async () => {
nock('http://gitlab.needtoken.com:3000')
.get('/api/v4/users?username=nodisplayname&private_token=faketoken')
.reply(200, [
{
username: 'nodisplayname',
name: 'No Display Name',
avatar_url:
'http://www.gravatar.com/avatar/3186450a99d1641bf75a44baa23f0826?s=80\u0026d=identicon',
web_url: 'https://gitlab.com/nodisplayname',
},
])
const info = await getUserInfo(
'nodisplayname',
'http://gitlab.needtoken.com:3000',
'faketoken',
)
expect(info.name).toBe('No Display Name')
})
test('handle error when no token is offered', async () => {
nock('http://gitlab.needtoken.com:3000')
.get('/api/v4/users?username=nodisplayname')
.reply(200, [])
await rejects(
getUserInfo('nodisplayname', 'http://gitlab.needtoken.com:3000', ''),
)
})