Delete items created for test modified
This commit is contained in:
@@ -5,27 +5,37 @@ import expect from 'must'
|
||||
|
||||
// ===================================================================
|
||||
|
||||
import {getConnection, getUser, deleteAllUsers} from './util.js'
|
||||
import {getConnection, getUser, createUser, deleteUsers} from './util.js'
|
||||
import {map, find} from 'lodash'
|
||||
|
||||
// ===================================================================
|
||||
|
||||
describe('group', function () {
|
||||
let xo
|
||||
let userIds = []
|
||||
let groupIds = []
|
||||
before(async function () {
|
||||
xo = await getConnection()
|
||||
})
|
||||
|
||||
afterEach(async function () {
|
||||
await deleteAllGroups()
|
||||
await deleteAllUsers(xo)
|
||||
await deleteGroups()
|
||||
await deleteUsers(xo, userIds)
|
||||
userIds = []
|
||||
})
|
||||
|
||||
async function deleteAllGroups () {
|
||||
async function createGroup (params) {
|
||||
const groupId = await xo.call('group.create', params)
|
||||
groupIds.push(groupId)
|
||||
return groupId
|
||||
}
|
||||
|
||||
async function deleteGroups () {
|
||||
await Promise.all(map(
|
||||
await getAllGroups(),
|
||||
group => xo.call('group.delete', {id: group.id})
|
||||
groupIds,
|
||||
groupId => xo.call('group.delete', {id: groupId})
|
||||
))
|
||||
groupIds = []
|
||||
}
|
||||
|
||||
function compareGroup (actual, expected) {
|
||||
@@ -48,7 +58,7 @@ describe('group', function () {
|
||||
describe('.create()', function () {
|
||||
|
||||
it('creates a group and return its id', async function () {
|
||||
const groupId = await xo.call('group.create', {
|
||||
const groupId = await createGroup({
|
||||
name: 'Avengers'
|
||||
})
|
||||
const group = await getGroup(groupId)
|
||||
@@ -61,15 +71,15 @@ describe('group', function () {
|
||||
})
|
||||
|
||||
it.skip('does not create two groups with the same name', async function () {
|
||||
await xo.call('group.create', {
|
||||
await createGroup({
|
||||
name: 'Avengers'
|
||||
})
|
||||
|
||||
await xo.call('group.create', {
|
||||
await createGroup({
|
||||
name: 'Avengers'
|
||||
}).then(
|
||||
function () {
|
||||
throw new Error('group.create() should have thrown')
|
||||
throw new Error('createGroup() should have thrown')
|
||||
},
|
||||
function (error) {
|
||||
expect(error.message).to.match(/duplicate group/i)
|
||||
@@ -83,7 +93,7 @@ describe('group', function () {
|
||||
describe('.delete()', function () {
|
||||
|
||||
it('delete a group', async function () {
|
||||
const groupId = await xo.call('group.create', {
|
||||
const groupId = await createGroup({
|
||||
name: 'Avengers'
|
||||
})
|
||||
|
||||
@@ -113,18 +123,18 @@ describe('group', function () {
|
||||
|
||||
it('can set users of a group', async function () {
|
||||
const [groupId, userId1, userId2, userId3] = await Promise.all([
|
||||
xo.call('group.create', {
|
||||
createGroup({
|
||||
name: 'Avengers'
|
||||
}),
|
||||
xo.call('user.create', {
|
||||
createUser(xo, userIds, {
|
||||
email: 'tony.stark@stark_industry.com',
|
||||
password: 'IronMan'
|
||||
}),
|
||||
xo.call('user.create', {
|
||||
createUser(xo, userIds, {
|
||||
email: 'natasha.romanov@shield.com',
|
||||
password: 'BlackWidow'
|
||||
}),
|
||||
xo.call('user.create', {
|
||||
createUser(xo, userIds, {
|
||||
email: 'pietro.maximoff@shield.com',
|
||||
password: 'QickSilver'
|
||||
})
|
||||
@@ -185,11 +195,11 @@ describe('group', function () {
|
||||
describe('.addUser()', function () {
|
||||
|
||||
it('adds a user id to a group', async function () {
|
||||
const groupId = await xo.call('group.create', {
|
||||
const groupId = await createGroup({
|
||||
name: 'Avengers'
|
||||
})
|
||||
|
||||
const userId = await xo.call('user.create', {
|
||||
const userId = await createUser(xo, userIds, {
|
||||
email: 'tony.stark@stark_industry.com',
|
||||
password: 'IronMan'
|
||||
})
|
||||
@@ -218,11 +228,11 @@ describe('group', function () {
|
||||
describe('removeUser()', function () {
|
||||
|
||||
it('removes a user to a group', async function () {
|
||||
const groupId = await xo.call('group.create', {
|
||||
const groupId = await createGroup({
|
||||
name: 'Avengers'
|
||||
})
|
||||
|
||||
const userId = await xo.call('user.create', {
|
||||
const userId = await createUser(xo, userIds, {
|
||||
email: 'tony.stark@stark_industry.com',
|
||||
password: 'IronMan'
|
||||
})
|
||||
@@ -254,7 +264,7 @@ describe('group', function () {
|
||||
|
||||
describe('set()', function () {
|
||||
it('changes name of a group', async function () {
|
||||
const groupId = await xo.call('group.create', {
|
||||
const groupId = await createGroup({
|
||||
name: 'Avengers'
|
||||
})
|
||||
|
||||
|
||||
@@ -9,17 +9,26 @@ import {map, find} from 'lodash'
|
||||
|
||||
describe('server', function () {
|
||||
let xo
|
||||
let serverIds = []
|
||||
before(async function () {
|
||||
xo = await getConnection()
|
||||
})
|
||||
|
||||
afterEach(async function () {
|
||||
await Promise.all(map(
|
||||
await getAllServers(),
|
||||
server => xo.call('server.remove', {id: server.id})
|
||||
serverIds,
|
||||
serverId => xo.call('server.remove', {id: serverId})
|
||||
))
|
||||
serverIds = []
|
||||
})
|
||||
|
||||
async function addServer (params) {
|
||||
const serverId = await xo.call('server.add', params)
|
||||
serverIds.push(serverId)
|
||||
console.log(serverIds)
|
||||
return serverId
|
||||
}
|
||||
|
||||
async function getAllServers () {
|
||||
return await xo.call('server.getAll')
|
||||
}
|
||||
@@ -33,7 +42,7 @@ describe('server', function () {
|
||||
|
||||
describe('.add()', function () {
|
||||
it('add a Xen server ans return its id', async function () {
|
||||
const serverId = await xo.call('server.add', {
|
||||
const serverId = await addServer({
|
||||
host: 'xen1.example.org',
|
||||
username: 'root',
|
||||
password: 'password',
|
||||
@@ -50,28 +59,28 @@ describe('server', function () {
|
||||
})
|
||||
|
||||
it.skip('does not create two servers with the same host', async function () {
|
||||
await xo.call('server.add', {
|
||||
await addServer({
|
||||
host: 'xen1.example.org',
|
||||
username: 'root',
|
||||
password: 'password',
|
||||
autoConnect: false
|
||||
})
|
||||
|
||||
await xo.call('server.add', {
|
||||
await addServer({
|
||||
host: 'xen1.example.org',
|
||||
username: 'root',
|
||||
password: 'password',
|
||||
autoConnect: false
|
||||
}).then(
|
||||
function () {
|
||||
throw new Error('server.add() should have thrown')
|
||||
throw new Error('addServer() should have thrown')
|
||||
},
|
||||
function (error) {
|
||||
expect(error.message).to.match(/dupplicate server/i)
|
||||
})
|
||||
})
|
||||
it('set autoConnect true by default', async function () {
|
||||
const serverId = await xo.call('server.add', {
|
||||
const serverId = await addServer({
|
||||
host: '192.168.1.3',
|
||||
username: 'root',
|
||||
password: 'qwerty'
|
||||
@@ -90,7 +99,7 @@ describe('server', function () {
|
||||
|
||||
describe('.remove()', function () {
|
||||
it('remove a Xen server', async function () {
|
||||
const serverId = await xo.call('server.add', {
|
||||
const serverId = await addServer({
|
||||
host: 'xen1.example.org',
|
||||
username: 'root',
|
||||
password: 'password',
|
||||
@@ -120,7 +129,7 @@ describe('server', function () {
|
||||
|
||||
describe('.set()', function () {
|
||||
it('chages attributes of an existinh server', async function () {
|
||||
const serverId = await xo.call('server.add', {
|
||||
const serverId = await addServer({
|
||||
host: 'xen1.example.org',
|
||||
username: 'root',
|
||||
password: 'password',
|
||||
@@ -146,9 +155,10 @@ describe('server', function () {
|
||||
// -----------------------------------------------------------------
|
||||
|
||||
describe('.connect()', function () {
|
||||
this.timeout(30e3)
|
||||
it('connects to a Xen server', async function () {
|
||||
// 192.168.1.3 root:qwerty
|
||||
const serverId = await xo.call('server.add', {
|
||||
const serverId = await addServer({
|
||||
host: '192.168.1.3',
|
||||
username: 'root',
|
||||
password: 'qwerty',
|
||||
@@ -175,7 +185,7 @@ describe('server', function () {
|
||||
describe('.disconnect()', function () {
|
||||
this.timeout(30000)
|
||||
it('disconnects to a Xen server', async function () {
|
||||
const serverId = await xo.call('server.add', {
|
||||
const serverId = await addServer({
|
||||
host: '192.168.1.3',
|
||||
username: 'root',
|
||||
password: 'qwerty',
|
||||
|
||||
@@ -6,33 +6,46 @@ import expect from 'must'
|
||||
// ===================================================================
|
||||
|
||||
import {getConnection} from './util.js'
|
||||
import {map} from 'lodash'
|
||||
|
||||
// ===================================================================
|
||||
|
||||
describe('token', function () {
|
||||
let xo
|
||||
let tokens = []
|
||||
before(async function () {
|
||||
xo = await getConnection()
|
||||
})
|
||||
|
||||
// TODO : delete tokens afterEach
|
||||
after(async function () {
|
||||
await Promise.all(map(
|
||||
tokens,
|
||||
token => xo.call('token.delete', {token})
|
||||
))
|
||||
})
|
||||
|
||||
async function createToken () {
|
||||
const token = await xo.call('token.create')
|
||||
tokens.push(token)
|
||||
return token
|
||||
}
|
||||
|
||||
// =================================================================
|
||||
|
||||
describe('.create()', function () {
|
||||
|
||||
it('creates a token string which can be used to sign in', async function () {
|
||||
const token = await xo.call('token.create')
|
||||
const token = await createToken()
|
||||
|
||||
await getConnection({credentials: {token}})
|
||||
})
|
||||
})
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
describe('.delete()', function () {
|
||||
it('deletes a token', async function () {
|
||||
const token = await xo.call('token.create')
|
||||
const token = await createToken()
|
||||
const xo2 = await getConnection({credentials: {token}})
|
||||
|
||||
await xo2.call('token.delete', {
|
||||
|
||||
@@ -5,18 +5,20 @@ import expect from 'must'
|
||||
|
||||
// ===================================================================
|
||||
|
||||
import {getConnection, getUser, deleteAllUsers} from './util'
|
||||
import {getConnection, getUser, createUser, deleteUsers} from './util'
|
||||
|
||||
// ===================================================================
|
||||
|
||||
describe('user', function () {
|
||||
let xo
|
||||
let userIds = []
|
||||
before(async function () {
|
||||
xo = await getConnection()
|
||||
})
|
||||
|
||||
afterEach(async function () {
|
||||
await deleteAllUsers(xo)
|
||||
await deleteUsers(xo, userIds)
|
||||
userIds = []
|
||||
})
|
||||
|
||||
// =================================================================
|
||||
@@ -24,10 +26,9 @@ describe('user', function () {
|
||||
describe('.create()', function () {
|
||||
|
||||
it('creates an user and returns its id', async function () {
|
||||
const userId = await xo.call('user.create', {
|
||||
const userId = await createUser(xo, userIds, {
|
||||
email: 'wayne@vates.fr',
|
||||
password: 'batman'
|
||||
})
|
||||
password: 'batman'})
|
||||
|
||||
expect(userId).to.be.a.string()
|
||||
|
||||
@@ -41,17 +42,17 @@ describe('user', function () {
|
||||
})
|
||||
|
||||
it.skip('does not create two users with the same email', async function () {
|
||||
await xo.call('user.create', {
|
||||
await createUser(xo, userIds, {
|
||||
email: 'wayne@vates.fr',
|
||||
password: 'batman'
|
||||
})
|
||||
|
||||
await xo.call('user.create', {
|
||||
await createUser(xo, userIds, {
|
||||
email: 'wayne@vates.fr',
|
||||
password: 'alfred'
|
||||
}).then(
|
||||
function () {
|
||||
throw new Error('user.create() should have thrown')
|
||||
throw new Error('createUser() should have thrown')
|
||||
},
|
||||
function (error) {
|
||||
expect(error.message).to.match(/duplicate user/i)
|
||||
@@ -60,7 +61,7 @@ describe('user', function () {
|
||||
})
|
||||
|
||||
it('can set the user permission', async function () {
|
||||
const userId = await xo.call('user.create', {
|
||||
const userId = await createUser(xo, userIds, {
|
||||
email: 'wayne@vates.fr',
|
||||
password: 'batman',
|
||||
permission: 'admin'
|
||||
@@ -76,7 +77,7 @@ describe('user', function () {
|
||||
})
|
||||
|
||||
it('allows the user to sign in', async function () {
|
||||
await xo.call('user.create', {
|
||||
await createUser(xo, userIds, {
|
||||
email: 'wayne@vates.fr',
|
||||
password: 'batman'
|
||||
})
|
||||
@@ -93,7 +94,7 @@ describe('user', function () {
|
||||
describe('.delete()', function () {
|
||||
|
||||
it('deletes an user', async function () {
|
||||
const userId = await xo.call('user.create', {
|
||||
const userId = await createUser(xo, userIds, {
|
||||
email: 'wayne@vates.fr',
|
||||
password: 'batman'
|
||||
})
|
||||
@@ -132,7 +133,7 @@ describe('user', function () {
|
||||
describe('.set()', function () {
|
||||
|
||||
it('changes password of an existing user', async function () {
|
||||
const userId = await xo.call('user.create', {
|
||||
const userId = await createUser(xo, userIds, {
|
||||
email: 'wayne@vates.fr',
|
||||
password: 'batman'
|
||||
})
|
||||
@@ -149,7 +150,7 @@ describe('user', function () {
|
||||
})
|
||||
|
||||
it('changes email adress of an existing user', async function () {
|
||||
const userId = await xo.call('user.create', {
|
||||
const userId = await createUser(xo, userIds, {
|
||||
email: 'wayne@vates.fr',
|
||||
password: 'batman'
|
||||
})
|
||||
|
||||
12
src/util.js
12
src/util.js
@@ -45,9 +45,15 @@ export async function getUser (xo, id) {
|
||||
return find(users, {id: id})
|
||||
}
|
||||
|
||||
export async function deleteAllUsers (xo) {
|
||||
export async function createUser (xo, userIds, params) {
|
||||
const userId = await xo.call('user.create', params)
|
||||
userIds.push(userId)
|
||||
return userId
|
||||
}
|
||||
|
||||
export async function deleteUsers (xo, userIds) {
|
||||
await Promise.all(map(
|
||||
await getAllUsers(xo),
|
||||
user => (user.id !== xo.user.id) && xo.call('user.delete', {id: user.id})
|
||||
userIds,
|
||||
userId => xo.call('user.delete', {id: userId})
|
||||
))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user