Skip to content

Chore/add tests #217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
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
777 changes: 756 additions & 21 deletions package-lock.json

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,34 @@
"progress": "2.0.3",
"semver": "7.6.3",
"shamsi-date-converter": "1.0.5",
"sinon": "^19.0.2",
"tar": "7.4.3",
"ua-parser-js": "1.0.38",
"update-notifier": "7.1.0",
"ws": "8.18.0"
},
"devDependencies": {
"@oclif/test": "^4.1.11",
"@types/async-retry": "^1.4.9",
"@types/bytes": "^3.1.5",
"@types/chai": "^5.0.1",
"@types/fs-extra": "^11.0.4",
"@types/inquirer": "^9.0.7",
"@types/jest": "^29.5.14",
"@types/node": "18.15.11",
"@types/progress": "^2.0.7",
"@types/semver": "^7.5.8",
"@types/sinon": "^17.0.4",
"@types/ua-parser-js": "^0.7.39",
"@types/update-notifier": "^6.0.8",
"@types/ws": "^8.5.13",
"chai": "^5.2.0",
"eslint": "^8.53.0",
"eslint-config-oclif": "^5.2.2",
"eslint-config-oclif-typescript": "^3.1.13",
"husky": "^9.1.7",
"mocha": "^11.1.0",
"nock": "^14.0.1",
"oclif": "^4.14.12",
"prettier": "^3.3.3",
"pretty-quick": "^4.0.0",
Expand Down Expand Up @@ -169,7 +176,7 @@
"format": "prettier \"**/*.ts\" \"**/*.js\" \"**/*.json\" --ignore-path ./.prettierignore --write",
"postpack": "rm -f oclif.manifest.json tsconfig.tsbuildinfo",
"prepack": "set -ex; rm -rf lib && tsc -b && oclif manifest && oclif readme",
"test": "NODE_OPTIONS=--experimental-vm-modules jest",
"test": "node --loader ts-node/esm ./node_modules/.bin/mocha --forbid-only --timeout 5000 \"test/**/*.test.ts\"",
"version": "oclif readme && git add README.md",
"readme": "oclif readme",
"prepare": "husky install",
Expand Down
14 changes: 9 additions & 5 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ export interface IProject {
project_id: string;
created_at: string;
isDeployed: boolean;
network?: {
_id: string;
name: string;
};
}

export interface IGetProjectsResponse {
Expand Down Expand Up @@ -123,14 +127,14 @@ export interface IProjectDetails {
bundlePlanID: string;
fixedIPStatus: string;
created_at: string;
node: {
_id: string;
IP: string;
};
hourlyPrice: number;
isDeployed: boolean;
reservedDiskSpace: number;
network: string;
readOnlyRootFilesystem: boolean;
network: {
_id: string;
name: string;
};
}

export interface IProjectDetailsResponse {
Expand Down
7 changes: 4 additions & 3 deletions src/commands/account/use.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,22 @@ export default class AccountUse extends Command {
async run() {
const { flags } = await this.parse(AccountUse);
const liara_json = await this.readGlobalConfig();

if (
!liara_json ||
!liara_json.accounts ||
Object.keys(liara_json.accounts).length === 0
) {
this.error(
"Please add your accounts via 'liara account:add' command, first."
"Please add your accounts via 'liara account:add' command, first.",
);
}

const name = flags.account || (await this.promptName(liara_json.accounts));
const selectedAccount = liara_json.accounts[name];
!selectedAccount &&
this.error(
`Could not find any account associated with this name ${name}.`
`Could not find any account associated with this name ${name}.`,
);

for (const account of Object.keys(liara_json.accounts)) {
Expand All @@ -45,7 +46,7 @@ export default class AccountUse extends Command {
JSON.stringify({
version: GLOBAL_CONF_VERSION,
accounts: liara_json.accounts,
})
}),
);
this.log(chalk.green('> Auth credentials changed.'));
}
Expand Down
6 changes: 4 additions & 2 deletions src/commands/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import AccountAdd from './account/add.js';
import AccountUse from './account/use.js';
import { createDebugLogger } from '../utils/output.js';
import { GLOBAL_CONF_PATH, GLOBAL_CONF_VERSION } from '../constants.js';
import ora from 'ora';

export default class Login extends Command {
static description = 'login to your account';
Expand All @@ -26,6 +27,7 @@ export default class Login extends Command {
};

async run() {
this.spinner = ora();
const { flags } = await this.parse(Login);
const debug = createDebugLogger(flags.debug);

Expand Down Expand Up @@ -75,7 +77,7 @@ export default class Login extends Command {
JSON.stringify({
accounts: currentAccounts,
version: GLOBAL_CONF_VERSION,
})
}),
);

this.spinner.succeed('You have logged in successfully.');
Expand All @@ -93,7 +95,7 @@ export default class Login extends Command {
debug(`${error.message}\n`);

this.spinner.fail(
'Cannot open browser. Browser unavailable or lacks permissions.'
'Cannot open browser. Browser unavailable or lacks permissions.',
);
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/types/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ export default interface INetwork {
_id: string;
name: string;
createdAt: string;
projectCount: number,
databaseCount: number
}
201 changes: 201 additions & 0 deletions test/app/create.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
import { runCommand } from '@oclif/test';
import nock from 'nock';
import { expect } from 'chai';
import { networks } from '../fixtures/networks/fixture.ts';

describe('app:create', function () {
const api = nock('https://api.iran.liara.ir');

beforeEach(() => {
api.get('/v1/networks').query({ teamID: '' }).reply(200, networks);
});

afterEach(() => {
api.done();
nock.cleanAll();
});

it('creates an app with the specified flags', async () => {
api
.post('/v1/projects/', {
name: 'test-app',
planID: 'small-g2',
platform: 'laravel',
bundlePlanID: 'standard',
network: networks.networks[0]._id,
readOnlyRootFilesystem: true,
})
.query({ teamID: '' })
.reply(200);

const { stdout } = await runCommand([
'app:create',
'--app',
'test-app',
'--platform',
'laravel',
'--plan',
'small-g2',
'--feature-plan',
'standard',
'--network',
networks.networks[0].name,
'--read-only',
'true',
]);

expect(stdout).to.equal(`App test-app created.\n`);
});

it('throws an error if app name already exists', async () => {
api
.post('/v1/projects/', {
name: 'test-app',
planID: 'small-g2',
platform: 'laravel',
bundlePlanID: 'standard',
network: networks.networks[0]._id,
readOnlyRootFilesystem: true,
})
.query({ teamID: '' })
.reply(409, {
statusCode: 409,
error: 'Conflict',
message: 'Project exists.',
data: null,
});

const { error } = await runCommand([
'app:create',
'--app',
'test-app',
'--platform',
'laravel',
'--plan',
'small-g2',
'--feature-plan',
'standard',
'--network',
networks.networks[0].name,
'--read-only',
'true',
]);

expect(error?.message).to.equal(
'The app already exists. Please use a unique name for your app.',
);
});

it('throws an error if the network is not found', async () => {
const { error } = await runCommand([
'app:create',
'--app',
'test-app',
'--platform',
'laravel',
'--plan',
'small-g2',
'--feature-plan',
'standard',
'--network',
'not-found',
'--read-only',
'true',
]);

expect(error?.message).to.equal('Network not-found not found.');
});

it('thorws an error if user select a feature plan that is not available for free plan', async () => {
const { error } = await runCommand([
'app:create',
'--app',
'test-app',
'--platform',
'laravel',
'--plan',
'free',
'--feature-plan',
'standard',
'--network',
networks.networks[0].name,
'--read-only',
'true',
]);

expect(error?.message).to.equal(
`Only "free" feature bundle plan is available for free plan.`,
);
});

it('throws an error if the user does not have enough balance', async () => {
api
.post('/v1/projects/', {
name: 'test-app',
planID: 'small-g2',
platform: 'laravel',
bundlePlanID: 'standard',
network: networks.networks[0]._id,
readOnlyRootFilesystem: true,
})
.query({ teamID: '' })
.reply(402);

const { error } = await runCommand([
'app:create',
'--app',
'test-app',
'--platform',
'laravel',
'--plan',
'small-g2',
'--feature-plan',
'standard',
'--network',
networks.networks[0].name,
'--read-only',
'true',
]);

expect(error?.message).to.equal(
'Not enough balance. Please charge your account.',
);
});

it('throws proper error if the app creation fails with status code 500', async () => {
api
.post('/v1/projects/', {
name: 'test-app',
planID: 'small-g2',
platform: 'laravel',
bundlePlanID: 'standard',
network: networks.networks[0]._id,
readOnlyRootFilesystem: true,
})
.query({ teamID: '' })
.reply(500);

const { error } = await runCommand([
'app:create',
'--app',
'test-app',
'--platform',
'laravel',
'--plan',
'small-g2',
'--feature-plan',
'standard',
'--network',
networks.networks[0].name,
'--read-only',
'true',
]);

expect(error?.message).to.equal(`Error: Unable to Create App
Please try the following steps:
1. Check your internet connection.
2. Ensure you have enough balance.
3. Try again later.
4. If you still have problems, please contact support by submitting a ticket at https://console.liara.ir/tickets`);
});
});
Loading