This repository has been archived by the owner on Dec 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest.js
187 lines (167 loc) · 5.18 KB
/
test.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
const test = require('ava')
const fs = require('fs')
const tempy = require('tempy')
const Homebase = require('@beaker/homebase')
const {createClient} = require('./')
var portCounter = 10000
function createServer (configData) {
var configPath = tempy.file({extension: 'yml'})
fs.writeFileSync(configPath, configData)
var config = new Homebase.HomebaseConfig(configPath)
config.canonical.ports = {
http: ++portCounter,
https: ++portCounter
}
var server = Homebase.start(config)
server.hostUrl = `http://127.0.0.1:${config.ports.http}`
return server
}
test.cb('login fails on wrong username or password', t => {
var server = createServer(`
webapi:
username: bob
password: hunter2
`)
createClient(server.hostUrl, (err, client) => {
if (err) throw err
// wrong password fails
client.login('bob', 'hunter3', err => {
t.truthy(err)
t.deepEqual(err.statusCode, 403)
server.close()
t.end()
})
})
})
test.cb('can get account info', t => {
var server = createServer(`
domain: test.com
webapi:
username: bob
password: hunter2
dats:
- url: dat://1f968afe867f06b0d344c11efc23591c7f8c5fb3b4ac938d6000f330f6ee2a03/
name: mysite
`)
createClient(server.hostUrl, {username: 'bob', password: 'hunter2'}, (err, client) => {
if (err) throw err
t.truthy(client.hasSession)
// can get account info
client.getAccount((err, res) => {
if (err) throw err
t.deepEqual(res.username, 'bob')
// can list dats
client.listDats((err, res) => {
if (err) throw err
t.deepEqual(res.items, [
{
url: 'dat://1f968afe867f06b0d344c11efc23591c7f8c5fb3b4ac938d6000f330f6ee2a03/',
name: 'mysite',
additionalUrls: [
'dat://mysite.test.com'
]
}
])
// logout
client.logout(err => {
if (err) throw err
t.falsy(client.hasSession)
server.close()
t.end()
})
})
})
})
})
test.cb('add & remove dats', t => {
var server = createServer(`
domain: test.com
webapi:
username: bob
password: hunter2
dats:
- url: dat://1f968afe867f06b0d344c11efc23591c7f8c5fb3b4ac938d6000f330f6ee2a03/
name: mysite
`)
createClient(server.hostUrl, {username: 'bob', password: 'hunter2'}, (err, client) => {
if (err) throw err
t.truthy(client.hasSession)
// add dat
client.addDat({
url: 'dat://868d6000f330f6967f06b3ee2a03811efc23591afe0d344cc7f8c5fb3b4ac91f/',
name: 'othersite',
domains: ['othersite.com']
}, (err) => {
if (err) throw err
// get dat (verify)
client.getDat('868d6000f330f6967f06b3ee2a03811efc23591afe0d344cc7f8c5fb3b4ac91f', (err, res) => {
if (err) throw err
t.deepEqual(res, {
url: 'dat://868d6000f330f6967f06b3ee2a03811efc23591afe0d344cc7f8c5fb3b4ac91f/',
name: 'othersite',
additionalUrls: [
'dat://othersite.test.com',
'dat://othersite.com'
]
})
// update dat
client.updateDat('868d6000f330f6967f06b3ee2a03811efc23591afe0d344cc7f8c5fb3b4ac91f', {
domains: ['othersite.com', 'other-site.com']
}, (err) => {
if (err) throw err
// get dat (verify)
client.getDat('868d6000f330f6967f06b3ee2a03811efc23591afe0d344cc7f8c5fb3b4ac91f', (err, res) => {
t.deepEqual(res, {
url: 'dat://868d6000f330f6967f06b3ee2a03811efc23591afe0d344cc7f8c5fb3b4ac91f/',
name: 'othersite',
additionalUrls: [
'dat://othersite.test.com',
'dat://othersite.com',
'dat://other-site.com'
]
})
// list dats
client.listDats((err, res) => {
t.deepEqual(res.items, [
{
url: 'dat://1f968afe867f06b0d344c11efc23591c7f8c5fb3b4ac938d6000f330f6ee2a03/',
name: 'mysite',
additionalUrls: [
'dat://mysite.test.com'
]
},
{
url: 'dat://868d6000f330f6967f06b3ee2a03811efc23591afe0d344cc7f8c5fb3b4ac91f/',
name: 'othersite',
additionalUrls: [
'dat://othersite.test.com',
'dat://othersite.com',
'dat://other-site.com'
]
}
])
// remove dat
client.removeDat('868d6000f330f6967f06b3ee2a03811efc23591afe0d344cc7f8c5fb3b4ac91f', err => {
if (err) throw err
// list dats
client.listDats((err, res) => {
t.deepEqual(res.items, [
{
url: 'dat://1f968afe867f06b0d344c11efc23591c7f8c5fb3b4ac938d6000f330f6ee2a03/',
name: 'mysite',
additionalUrls: [
'dat://mysite.test.com'
]
}
])
server.close()
t.end()
})
})
})
})
})
})
})
})
})