2020-01-15 19:41:47 +00:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright 2019-2020 Balena Ltd.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2019-12-11 12:18:58 +00:00
|
|
|
import { expect } from 'chai';
|
2020-01-15 19:41:47 +00:00
|
|
|
import * as path from 'path';
|
|
|
|
|
2020-01-24 01:21:51 +00:00
|
|
|
import { apiResponsePath, BalenaAPIMock } from '../../balena-api-mock';
|
2019-12-11 12:18:58 +00:00
|
|
|
import { cleanOutput, runCommand } from '../../helpers';
|
|
|
|
|
|
|
|
const HELP_RESPONSE = `
|
2020-06-17 13:46:25 +00:00
|
|
|
List all devices.
|
2019-12-11 12:18:58 +00:00
|
|
|
|
2020-06-17 13:46:25 +00:00
|
|
|
USAGE
|
|
|
|
$ balena devices
|
2019-12-11 12:18:58 +00:00
|
|
|
|
2020-06-17 13:46:25 +00:00
|
|
|
OPTIONS
|
|
|
|
-a, --application <application> application name
|
|
|
|
-h, --help show CLI help
|
|
|
|
--app <app> same as '--application'
|
2019-12-11 12:18:58 +00:00
|
|
|
|
2020-06-17 13:46:25 +00:00
|
|
|
DESCRIPTION
|
|
|
|
list all devices that belong to you.
|
2019-12-11 12:18:58 +00:00
|
|
|
|
2020-06-17 13:46:25 +00:00
|
|
|
You can filter the devices by application by using the \`--application\` option.
|
2019-12-11 12:18:58 +00:00
|
|
|
|
2020-06-17 13:46:25 +00:00
|
|
|
EXAMPLES
|
|
|
|
$ balena devices
|
|
|
|
$ balena devices --application MyApp
|
|
|
|
$ balena devices --app MyApp
|
|
|
|
$ balena devices -a MyApp
|
2019-12-11 12:18:58 +00:00
|
|
|
`;
|
|
|
|
|
2020-06-15 22:53:07 +00:00
|
|
|
describe('balena devices', function () {
|
2019-12-11 12:18:58 +00:00
|
|
|
let api: BalenaAPIMock;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
api = new BalenaAPIMock();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
// Check all expected api calls have been made and clean up.
|
|
|
|
api.done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should print help text with the -h flag', async () => {
|
2020-06-17 13:46:25 +00:00
|
|
|
api.expectGetWhoAmI({ optional: true, persist: true });
|
2020-01-15 19:41:47 +00:00
|
|
|
api.expectGetMixpanel({ optional: true });
|
2019-12-11 12:18:58 +00:00
|
|
|
|
|
|
|
const { out, err } = await runCommand('devices -h');
|
|
|
|
|
|
|
|
expect(cleanOutput(out)).to.deep.equal(cleanOutput([HELP_RESPONSE]));
|
|
|
|
|
|
|
|
expect(err).to.eql([]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should list devices from own and collaborator apps', async () => {
|
2020-06-15 22:53:04 +00:00
|
|
|
api.expectGetWhoAmI({ optional: true, persist: true });
|
2020-01-15 19:41:47 +00:00
|
|
|
api.expectGetMixpanel({ optional: true });
|
2019-12-11 12:18:58 +00:00
|
|
|
|
|
|
|
api.scope
|
|
|
|
.get(
|
|
|
|
'/v5/device?$orderby=device_name%20asc&$expand=belongs_to__application($select=app_name)',
|
|
|
|
)
|
2020-01-15 19:41:47 +00:00
|
|
|
.replyWithFile(200, path.join(apiResponsePath, 'devices.json'), {
|
2019-12-11 12:18:58 +00:00
|
|
|
'Content-Type': 'application/json',
|
|
|
|
});
|
|
|
|
|
|
|
|
const { out, err } = await runCommand('devices');
|
|
|
|
|
|
|
|
const lines = cleanOutput(out);
|
|
|
|
|
|
|
|
expect(lines[0].replace(/ +/g, ' ')).to.equal(
|
|
|
|
'ID UUID DEVICE NAME DEVICE TYPE APPLICATION NAME STATUS ' +
|
|
|
|
'IS ONLINE SUPERVISOR VERSION OS VERSION DASHBOARD URL',
|
|
|
|
);
|
|
|
|
expect(lines).to.have.lengthOf.at.least(2);
|
|
|
|
|
2020-06-15 22:53:07 +00:00
|
|
|
expect(lines.some((l) => l.includes('test app'))).to.be.true;
|
2019-12-11 12:18:58 +00:00
|
|
|
|
|
|
|
// Devices with missing applications will have application name set to `N/a`.
|
|
|
|
// e.g. When user has a device associated with app that user is no longer a collaborator of.
|
2020-06-15 22:53:07 +00:00
|
|
|
expect(lines.some((l) => l.includes('N/a'))).to.be.true;
|
2019-12-11 12:18:58 +00:00
|
|
|
|
|
|
|
expect(err).to.eql([]);
|
|
|
|
});
|
|
|
|
});
|