/
create-test-user.js
86 lines (69 loc) · 2.35 KB
/
create-test-user.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
#!/usr/bin/env node
/**
* Make a user in the database and generate an access token for it
* @param {String} username Username for the new user
* @param {String} gitToken Git access token for the user
*/
'use strict';
/* eslint-disable import/no-dynamic-require */
/* eslint-disable no-console */
// Make sure script is being called correctly
if (process.argv.length !== 4) {
console.log('Usage: npm run create-test-user -- $username $scm-context $git-token');
return 1;
}
const username = process.argv[2];
const scmContext = process.argv[3];
const gitToken = process.argv[4];
const config = require('config');
const hoek = require('hoek');
// Setup Authentication
const authConfig = config.get('auth');
// Setup HTTPd
const httpdConfig = config.get('httpd');
// Special urls for things like the UI
const ecosystem = config.get('ecosystem');
ecosystem.api = httpdConfig.uri;
// Setup Datastore
const datastoreConfig = config.get('datastore');
const DatastorePlugin = require(`screwdriver-datastore-${datastoreConfig.plugin}`);
const datastore = new DatastorePlugin(hoek.applyToDefaults({ ecosystem },
(datastoreConfig[datastoreConfig.plugin] || {})));
// Source Code Plugin
const scmConfig = config.get('scm');
const ScmPlugin = require(`screwdriver-scm-${scmConfig.plugin}`);
const scm = new ScmPlugin(hoek.applyToDefaults({ ecosystem },
(scmConfig[scmConfig.plugin] || {})));
authConfig.scm = scm;
// Setup Model Factories
const Models = require('screwdriver-models');
const userFactory = Models.UserFactory.getInstance({
datastore,
scm,
password: authConfig.encryptionPassword
});
const tokenFactory = Models.TokenFactory.getInstance({
datastore
});
// Setup datastore and create test user
return datastore.setup()
.then(() => userFactory.get({ username, scmContext }))
.then((model) => {
if (!model) {
return userFactory.create({
username,
scmContext,
token: gitToken
});
}
return model.sealToken(gitToken)
.then((token) => {
model.token = token;
return model.update();
});
})
.then(testUser => tokenFactory.create({
name: 'Functional test token',
userId: testUser.id
}))
.then(token => console.log(`Token created for user ${username}: ${token.value}`));