Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions test/integration/controllers/DeviceTypeAPI.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ const TEST_DEVICE_TYPE_NAME_PREFIX = `DH-JS-LIB-DEVICE-TYPE-NAME-`;
const TEST_DEVICE_TYPE_DESCRIPTION_PREFIX = `DH-JS-LIB-DEVICE-TYPE-NAME-`;
const TEST_DEVICE_TYPES = {
HTTP: {
name: `${TEST_DEVICE_TYPE_NAME_PREFIX}-${randomString.generate()}`,
description: `${TEST_DEVICE_TYPE_DESCRIPTION_PREFIX}-${randomString.generate()}`
name: `${TEST_DEVICE_TYPE_NAME_PREFIX}${randomString.generate()}`,
description: `${TEST_DEVICE_TYPE_DESCRIPTION_PREFIX}${randomString.generate()}`
},
WS: {
name: `${TEST_DEVICE_TYPE_NAME_PREFIX}-${randomString.generate()}`,
description: `${TEST_DEVICE_TYPE_DESCRIPTION_PREFIX}-${randomString.generate()}`
name: `${TEST_DEVICE_TYPE_NAME_PREFIX}${randomString.generate()}`,
description: `${TEST_DEVICE_TYPE_DESCRIPTION_PREFIX}${randomString.generate()}`
}
};

Expand Down
8 changes: 4 additions & 4 deletions test/integration/controllers/NetworkAPI.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ const TEST_NETWORK_NAME_PREFIX = `DH-JS-LIB-NETWORK-NAME-`;
const TEST_NETWORK_DESCRIPTION_PREFIX = `DH-JS-LIB-NETWORK-NAME-`;
const TEST_NETWORKS = {
HTTP: {
name: `${TEST_NETWORK_NAME_PREFIX}-${randomString.generate()}`,
description: `${TEST_NETWORK_DESCRIPTION_PREFIX}-${randomString.generate()}`
name: `${TEST_NETWORK_NAME_PREFIX}${randomString.generate()}`,
description: `${TEST_NETWORK_DESCRIPTION_PREFIX}${randomString.generate()}`
},
WS: {
name: `${TEST_NETWORK_NAME_PREFIX}-${randomString.generate()}`,
description: `${TEST_NETWORK_DESCRIPTION_PREFIX}-${randomString.generate()}`
name: `${TEST_NETWORK_NAME_PREFIX}${randomString.generate()}`,
description: `${TEST_NETWORK_DESCRIPTION_PREFIX}${randomString.generate()}`
}
};

Expand Down
176 changes: 111 additions & 65 deletions test/integration/controllers/UserAPI.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const randomString = require(`randomstring`);
const chai = require(`chai`);
const assert = chai.assert;
const expect = chai.expect;
const config = require(`../config`);
const DeviceHive = require(`../../../index`);
const User = DeviceHive.models.User;
const DeviceType = DeviceHive.models.DeviceType;
const Network = DeviceHive.models.Network;
const UserListQuery = DeviceHive.models.query.UserListQuery;
const UserCountQuery = DeviceHive.models.query.UserCountQuery;

Expand All @@ -14,29 +17,47 @@ const wsDeviceHive = new DeviceHive(config.server.ws);
const TEST_USER_LOGIN_PREFIX = `DH-JS-LIB-USER-LOGIN-`;
const TEST_USERS = {
HTTP: {
login: `${TEST_USER_LOGIN_PREFIX}-${randomString.generate()}`,
login: `${TEST_USER_LOGIN_PREFIX}${randomString.generate()}`,
role: 1,
status: 1,
status: 0,
password: `password`,
data: {
jsonString: `jsonString`
}
},
WS: {
login: `${TEST_USER_LOGIN_PREFIX}-${randomString.generate()}`,
login: `${TEST_USER_LOGIN_PREFIX}${randomString.generate()}`,
role: 1,
status: 1,
status: 0,
password: `password`,
data: {
jsonString: `jsonString`
}
}
};
const TEST_DEVICE_TYPE_NAME_PREFIX = `DH-JS-LIB-DEVICE-TYPE-NAME-`;
const TEST_DEVICE_TYPE_DESCRIPTION_PREFIX = `DH-JS-LIB-DEVICE-TYPE-NAME-`;
const TEST_DEVICE_TYPE = {
name: `${TEST_DEVICE_TYPE_NAME_PREFIX}${randomString.generate()}`,
description: `${TEST_DEVICE_TYPE_DESCRIPTION_PREFIX}${randomString.generate()}`
};
const TEST_NETWORK_NAME_PREFIX = `DH-JS-LIB-NETWORK-NAME-`;
const TEST_NETWORK_DESCRIPTION_PREFIX = `DH-JS-LIB-NETWORK-NAME-`;
const TEST_NETWORK = {
name: `${TEST_NETWORK_NAME_PREFIX}${randomString.generate()}`,
description: `${TEST_NETWORK_DESCRIPTION_PREFIX}${randomString.generate()}`
};

describe(`UserAPI`, () => {
const deviceTypeModel = new DeviceType(TEST_DEVICE_TYPE);
const networkModel = new Network(TEST_NETWORK);

before(done => {
Promise.all([httpDeviceHive.connect(), wsDeviceHive.connect()])
.then(() => httpDeviceHive.deviceType.insert(deviceTypeModel))
.then(({ id }) => TEST_DEVICE_TYPE.id = id)
.then(() => httpDeviceHive.network.insert(networkModel))
.then(({ id }) => TEST_NETWORK.id = id)
.then(() => done());
});

Expand Down Expand Up @@ -88,19 +109,19 @@ describe(`UserAPI`, () => {
.catch(done);
});

it(`should get user with name: ${TEST_USERS.HTTP.name} via HTTP`, done => {
it(`should get user with login: ${TEST_USERS.HTTP.login} via HTTP`, done => {
httpDeviceHive.user.get(TEST_USERS.HTTP.id)
.then(user => {
assert.equal(user.name, TEST_USERS.HTTP.name);
assert.equal(user.login, TEST_USERS.HTTP.login);
})
.then(() => done())
.catch(done);
});

it(`should get user with name: ${TEST_USERS.WS.name} via WS`, done => {
it(`should get user with login: ${TEST_USERS.WS.login} via WS`, done => {
wsDeviceHive.user.get(TEST_USERS.WS.id)
.then(user => {
assert.equal(user.name, TEST_USERS.WS.name);
assert.equal(user.login, TEST_USERS.WS.login);
})
.then(() => done())
.catch(done);
Expand Down Expand Up @@ -153,24 +174,24 @@ describe(`UserAPI`, () => {
});

it(`should update current user with login ${config.TEST_USER_LOGIN} via HTTP`, done => {
const userModel = new User({ status: 0 });
const userModel = new User({ data: { update: true } });

httpDeviceHive.user.updateCurrent(userModel)
.then(() => httpDeviceHive.user.getCurrent())
.then(user => {
assert.equal(user.status, userModel.status);
assert.deepEqual(user.data, userModel.data);
})
.then(() => done())
.catch(done);
});

it(`should update current user with login ${config.TEST_USER_LOGIN} via WS`, done => {
const userModel = new User({ status: 1 });
const userModel = new User({ data: { update: true } });

wsDeviceHive.user.updateCurrent(userModel)
.then(() => wsDeviceHive.user.getCurrent())
.then(user => {
assert.equal(user.status, userModel.status);
assert.deepEqual(user.data, userModel.data);
})
.then(() => done())
.catch(done);
Expand Down Expand Up @@ -198,103 +219,128 @@ describe(`UserAPI`, () => {
.catch(done);
});

it(`UserAPI.getDeviceTypes()`, done => {

Promise.all([httpDeviceHive.user.getDeviceTypes(testUsers[0].id)])
it(`should get user's deviceTypes via HTTP`, done => {
httpDeviceHive.user.getDeviceTypes(TEST_USERS.HTTP.id)
.then(deviceTypes => {
assert.exists(deviceTypes);
expect(deviceTypes).to.be.an('array');
})
.then(() => done())
.catch(done);
});


it(`UserAPI.assignAllDeviceTypes()`, done => {

Promise.all([httpDeviceHive.user.assignAllDeviceTypes(testUsers[0].id)])
it(`should unassign all device types from user with login: ${TEST_USERS.HTTP.login} via HTTP`, done => {
httpDeviceHive.user.unassignAllDeviceTypes(TEST_USERS.HTTP.id)
.then(() => httpDeviceHive.user.getDeviceTypes(TEST_USERS.HTTP.id))
.then((deviceTypes) => {
assert.exists(deviceTypes);
expect(deviceTypes).to.be.an('array').that.is.empty;
})
.then(() => done())
.catch(done);
});


it(`UserAPI.unassignAllDeviceTypes()`, done => {

Promise.all([httpDeviceHive.user.unassignAllDeviceTypes(testUsers[0].id)])
it(`should assign all device types to user with login: ${TEST_USERS.HTTP.login} via HTTP`, done => {
httpDeviceHive.user.assignAllDeviceTypes(TEST_USERS.HTTP.id)
.then(() => httpDeviceHive.user.getDeviceTypes(TEST_USERS.HTTP.id))
.then((deviceTypes) => {
assert.exists(deviceTypes);
expect(deviceTypes).to.be.an('array').that.is.not.empty;
})
.then(() => done())
.catch(done);
});


it(`UserAPI.assignDeviceType()`, done => {

Promise.all([httpDeviceHive.user.assignDeviceType(testUsers[0].id, 1)])
it(`should assign device type with name: ${TEST_DEVICE_TYPE.name} to user with login: ${TEST_USERS.HTTP.login} via HTTP`, done => {
httpDeviceHive.user.unassignAllDeviceTypes(TEST_USERS.HTTP.id)
.then(() => httpDeviceHive.user.assignDeviceType(TEST_USERS.HTTP.id, TEST_DEVICE_TYPE.id))
.then(() => done())
.catch(done);
});


it(`UserAPI.getDeviceType()`, done => {

Promise.all([httpDeviceHive.user.getDeviceType(testUsers[0].id, 1)])
.then(dataAll => {
for (const data of dataAll) {
assert.isObject(data);
assert.property(data, `deviceType`);
}
it(`should get users device type with name: ${TEST_DEVICE_TYPE.name} via HTTP`, done => {
httpDeviceHive.user.getDeviceType(TEST_USERS.HTTP.id, TEST_DEVICE_TYPE.id)
.then(({ deviceType }) => {
assert.exists(deviceType);
assert.equal(deviceType.name, TEST_DEVICE_TYPE.name);
})
.then(done)
.then(() => done())
.catch(done);
});

it(`should unassign device type with name: ${TEST_DEVICE_TYPE.name} from user with login: ${TEST_USERS.HTTP.login} via HTTP`, done => {
httpDeviceHive.user.unassignDeviceType(TEST_USERS.HTTP.id, TEST_DEVICE_TYPE.id)
.then(() => httpDeviceHive.user.getDeviceType(TEST_USERS.HTTP.id, TEST_DEVICE_TYPE.id))
.then(() => done(new Error(`Device type with id ${TEST_DEVICE_TYPE.id} for user with id ${TEST_USERS.HTTP.id} should not be found`)))
.catch(() => done());
});

it(`UserAPI.unassignDeviceType()`, done => {

Promise.all([httpDeviceHive.user.unassignDeviceType(testUsers[0].id, 1)])
it(`should assign network with name: ${TEST_NETWORK.name} to user with login: ${TEST_USERS.HTTP.login} via HTTP`, done => {
httpDeviceHive.user.assignNetwork(TEST_USERS.HTTP.id, TEST_NETWORK.id)
.then(() => done())
.catch(done);
});


it(`UserAPI.assignNetwork()`, done => {

httpDeviceHive.user.assignNetwork(testUsers[0].id, 1)
.then(() => wsDeviceHive.user.assignNetwork(testUsers[1].id, 1))
it(`should get users network with name: ${TEST_NETWORK.name} via HTTP`, done => {
httpDeviceHive.user.getNetwork(TEST_USERS.HTTP.id, TEST_NETWORK.id)
.then(({ network }) => {
assert.exists(network);
assert.equal(network.name, TEST_NETWORK.name);
})
.then(() => done())
.catch(done);
});

it(`should unassign network with name: ${TEST_NETWORK.name} from user with login: ${TEST_USERS.HTTP.login} via HTTP`, done => {
httpDeviceHive.user.unassignNetwork(TEST_USERS.HTTP.id, TEST_NETWORK.id)
.then(() => httpDeviceHive.user.getNetwork(TEST_USERS.HTTP.id, TEST_NETWORK.id))
.then(() => done(new Error(`Network with id ${TEST_NETWORK.id} for user with id ${TEST_USERS.HTTP.id} should not be found`)))
.catch(() => done());
});

it(`UserAPI.getNetwork()`, done => {
it(`should assign network with name: ${TEST_NETWORK.name} to user with login: ${TEST_USERS.WS.login} via WS`, done => {
wsDeviceHive.user.assignNetwork(TEST_USERS.WS.id, TEST_NETWORK.id)
.then(() => done())
.catch(done);
});

Promise.all([httpDeviceHive.user.getNetwork(testUsers[0].id, 1), wsDeviceHive.user.getNetwork(testUsers[1].id, 1)])
.then(dataAll => {
for (const data of dataAll) {
assert.isObject(data);
assert.property(data, `network`);
}
it(`should get users network with name: ${TEST_NETWORK.name} via WS`, done => {
wsDeviceHive.user.getNetwork(TEST_USERS.WS.id, TEST_NETWORK.id)
.then(({ network }) => {
assert.exists(network);
assert.equal(network.name, TEST_NETWORK.name);
})
.then(done)
.then(() => done())
.catch(done);
});

it(`should unassign network with name: ${TEST_NETWORK.name} from user with login: ${TEST_USERS.WS.login} via WS`, done => {
wsDeviceHive.user.unassignNetwork(TEST_USERS.WS.id, TEST_NETWORK.id)
.then(() => wsDeviceHive.user.getNetwork(TEST_USERS.WS.id, TEST_NETWORK.id))
.then(() => done(new Error(`Network with id ${TEST_NETWORK.id} for user with id ${TEST_USERS.WS.id} should not be found`)))
.catch(() => done());
});

it(`UserAPI.unassignNetwork()`, done => {

httpDeviceHive.user.unassignNetwork(testUsers[0].id, 1)
.then(() => wsDeviceHive.user.unassignNetwork(testUsers[1].id, 1))
it(`should delete user with login: ${TEST_USERS.HTTP.login} via HTTP`, done => {
httpDeviceHive.user.delete(TEST_USERS.HTTP.id)
.then(() => done())
.catch(done);
});


it(`UserAPI.delete()`, done => {

Promise.all([httpDeviceHive.user.delete(testUsers[0].id), wsDeviceHive.user.delete(testUsers[1].id)])
it(`should delete user with login: ${TEST_USERS.WS.login} via WS`, done => {
wsDeviceHive.user.delete(TEST_USERS.WS.id)
.then(() => done())
.catch(done);
});

after(done => {
httpDeviceHive.disconnect();
wsDeviceHive.disconnect();

done();
httpDeviceHive.deviceType.delete(TEST_DEVICE_TYPE.id)
.then(() => httpDeviceHive.network.delete(TEST_NETWORK.id))
.then(() => {
httpDeviceHive.disconnect();
wsDeviceHive.disconnect();

done();
});
});
});