Skip to content
This repository was archived by the owner on May 31, 2022. It is now read-only.

Commit c45164c

Browse files
committed
add connection.title
1 parent 585e9b0 commit c45164c

File tree

2 files changed

+145
-18
lines changed

2 files changed

+145
-18
lines changed

lib/extended-model.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,32 @@ const ExtendedConnection = Connection.extend(storageMixin, {
6969
return this.ldapUsername;
7070
}
7171
}
72+
},
73+
title: {
74+
deps: [
75+
'name',
76+
'isFavorite',
77+
'isSrvRecord',
78+
'hostname',
79+
'hosts'
80+
],
81+
fn() {
82+
if (this.isFavorite && this.name) {
83+
return this.name;
84+
}
85+
86+
if (this.isSrvRecord) {
87+
return this.hostname;
88+
}
89+
90+
if (this.hosts && this.hosts.length) {
91+
return this.hosts.map(
92+
({ host, port }) => `${host}:${port}`
93+
).join(',');
94+
}
95+
96+
return this.hostname;
97+
}
7298
}
7399
},
74100
serialize() {

test/model.test.js

Lines changed: 119 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,147 @@ const assert = require('assert');
22
const Connection = require('../');
33

44
describe('Connection', () => {
5+
describe('title', () => {
6+
it('works with default host', () => {
7+
assert.strictEqual(
8+
new Connection({}).title,
9+
'localhost:27017'
10+
);
11+
});
12+
13+
it('returns the hostname if the connection is srv', () => {
14+
assert.strictEqual(
15+
new Connection({
16+
isSrvRecord: true,
17+
hostname: 'somehost'
18+
}).title,
19+
'somehost'
20+
);
21+
});
22+
23+
it('returns hosts if the connection is not srv', () => {
24+
assert.strictEqual(
25+
new Connection({ hosts: [{ host: 'example.com', port: 12345 }] }).title,
26+
'example.com:12345'
27+
);
28+
});
29+
30+
it('returns the name of the favorite if connection is favorite', () => {
31+
assert.strictEqual(
32+
new Connection({
33+
isFavorite: true,
34+
name: 'Favorite Name'
35+
}).title,
36+
'Favorite Name'
37+
);
38+
});
39+
40+
it('falls back to hostname if nothing else match', () => {
41+
assert.strictEqual(
42+
new Connection({
43+
isSrvRecord: false,
44+
isFavorite: false,
45+
hosts: [],
46+
hostname: 'somehost'
47+
}).title,
48+
'somehost'
49+
);
50+
});
51+
});
52+
53+
describe('username', () => {
54+
it('returns empty by default', () => {
55+
assert.strictEqual(
56+
new Connection({}).username,
57+
''
58+
);
59+
});
60+
61+
it('returns empty if authStrategy is NONE', () => {
62+
assert.strictEqual(
63+
new Connection({
64+
authStrategy: 'NONE'
65+
}).username,
66+
''
67+
);
68+
});
69+
70+
[
71+
['MONGODB', 'mongodbUsername'],
72+
['KERBEROS', 'kerberosPrincipal'],
73+
['X509', 'x509Username'],
74+
['LDAP', 'ldapUsername']
75+
].forEach(
76+
([authStrategy, property]) => {
77+
describe(`when authStrategy is ${authStrategy}`, () => {
78+
let connection;
79+
80+
beforeEach(() => {
81+
connection = new Connection({
82+
authStrategy,
83+
[property]: 'value-1'
84+
});
85+
});
86+
87+
it(`returns ${property}`, () => {
88+
assert.strictEqual(
89+
connection.username,
90+
'value-1'
91+
);
92+
});
93+
94+
it('updates with dependencies', () => {
95+
connection.set({ [property]: 'value-2' });
96+
assert.strictEqual(
97+
connection.username,
98+
'value-2'
99+
);
100+
});
101+
});
102+
}
103+
);
104+
});
105+
5106
describe('#parse', () => {
6107
context('when the attributes have legacy passwords', () => {
7108
context('when the attributes have no new passwords', () => {
8109
it('maps mongodb_password', () => {
9-
assert.equal(
110+
assert.strictEqual(
10111
new Connection({ mongodb_password: 'test' }).mongodbPassword,
11112
'test'
12113
);
13114
});
14115

15116
it('maps kerberos_password', () => {
16-
assert.equal(
117+
assert.strictEqual(
17118
new Connection({ kerberos_password: 'test' }).kerberosPassword,
18119
'test'
19120
);
20121
});
21122

22123
it('maps ldap_password', () => {
23-
assert.equal(
124+
assert.strictEqual(
24125
new Connection({ ldap_password: 'test' }).ldapPassword,
25126
'test'
26127
);
27128
});
28129

29130
it('maps ssl_private_key_password', () => {
30-
assert.equal(
131+
assert.strictEqual(
31132
new Connection({ ssl_private_key_password: 'test' }).sslPass,
32133
'test'
33134
);
34135
});
35136

36137
it('maps ssh_tunnel_password', () => {
37-
assert.equal(
138+
assert.strictEqual(
38139
new Connection({ ssh_tunnel_password: 'test' }).sshTunnelPassword,
39140
'test'
40141
);
41142
});
42143

43144
it('maps ssh_tunnel_passphrase', () => {
44-
assert.equal(
145+
assert.strictEqual(
45146
new Connection({ ssh_tunnel_passphrase: 'test' })
46147
.sshTunnelPassphrase,
47148
'test'
@@ -51,42 +152,42 @@ describe('Connection', () => {
51152

52153
context('when the attributes have falsey values', () => {
53154
it('does not map mongodb_password', () => {
54-
assert.equal(
155+
assert.strictEqual(
55156
new Connection({ mongodb_password: '' }).mongodbPassword,
56157
undefined
57158
);
58159
});
59160

60161
it('does not map kerberos_password', () => {
61-
assert.equal(
162+
assert.strictEqual(
62163
new Connection({ kerberos_password: '' }).kerberosPassword,
63164
undefined
64165
);
65166
});
66167

67168
it('does not map ldap_password', () => {
68-
assert.equal(
169+
assert.strictEqual(
69170
new Connection({ ldap_password: '' }).ldapPassword,
70171
undefined
71172
);
72173
});
73174

74175
it('does not map ssl_private_key_password', () => {
75-
assert.equal(
176+
assert.strictEqual(
76177
new Connection({ ssl_private_key_password: '' }).sslPass,
77178
undefined
78179
);
79180
});
80181

81182
it('does not map ssh_tunnel_password', () => {
82-
assert.equal(
183+
assert.strictEqual(
83184
new Connection({ ssh_tunnel_password: '' }).sshTunnelPassword,
84185
undefined
85186
);
86187
});
87188

88189
it('does not map ssh_tunnel_passphrase', () => {
89-
assert.equal(
190+
assert.strictEqual(
90191
new Connection({ ssh_tunnel_passphrase: '' }).sshTunnelPassphrase,
91192
undefined
92193
);
@@ -95,15 +196,15 @@ describe('Connection', () => {
95196

96197
context('when the attributes have new passwords', () => {
97198
it('does not map mongodb_password', () => {
98-
assert.equal(
199+
assert.strictEqual(
99200
new Connection({ mongodb_password: 'test', mongodbPassword: 'pw' })
100201
.mongodbPassword,
101202
'pw'
102203
);
103204
});
104205

105206
it('does not map kerberos_password', () => {
106-
assert.equal(
207+
assert.strictEqual(
107208
new Connection({
108209
kerberos_password: 'test',
109210
kerberosPassword: 'pw'
@@ -113,23 +214,23 @@ describe('Connection', () => {
113214
});
114215

115216
it('does not map ldap_password', () => {
116-
assert.equal(
217+
assert.strictEqual(
117218
new Connection({ ldap_password: 'test', ldapPassword: 'pw' })
118219
.ldapPassword,
119220
'pw'
120221
);
121222
});
122223

123224
it('does not map ssl_private_key_password', () => {
124-
assert.equal(
225+
assert.strictEqual(
125226
new Connection({ ssl_private_key_password: 'test', sslPass: 'pw' })
126227
.sslPass,
127228
'pw'
128229
);
129230
});
130231

131232
it('does not map ssh_tunnel_password', () => {
132-
assert.equal(
233+
assert.strictEqual(
133234
new Connection({
134235
ssh_tunnel_password: 'test',
135236
sshTunnelPassword: 'pw'
@@ -139,7 +240,7 @@ describe('Connection', () => {
139240
});
140241

141242
it('does not map ssh_tunnel_passphrase', () => {
142-
assert.equal(
243+
assert.strictEqual(
143244
new Connection({
144245
ssh_tunnel_passphrase: 'test',
145246
sshTunnelPassphrase: 'pw'

0 commit comments

Comments
 (0)