Skip to content

Commit

Permalink
Merge branch 'master' into api-refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
brollb committed Dec 1, 2023
2 parents 1257a52 + a5e6a36 commit d012593
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 78 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "netsblox-cloud-client",
"version": "2.0.0",
"version": "2.1.0",
"description": "JS client for the netsblox cloud",
"main": "index.js",
"scripts": {
Expand Down
17 changes: 11 additions & 6 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export default class Cloud {
return await response.json();
}

async renameRole(roleId, name) {
async renameRole(roleId, name: string) {
const body = {
name,
clientId: this.clientId,
Expand All @@ -209,12 +209,13 @@ export default class Cloud {
//return await response.json();
}

async renameProject(name) {
async renameProject(name: string): Promise<ProjectMetadata> {
const body = {
name,
clientId: this.clientId,
};
const response = await this.patch(`/projects/id/${this.projectId}`, body);
return await response.json();
}

async reportLatestRole(id, data) {
Expand Down Expand Up @@ -307,19 +308,23 @@ export default class Cloud {
return project;
}

async getProjectByName(owner, name) {
const response = await this.fetch(`/projects/user/${owner}/${name}`);
async getProjectByName(owner: string, name: string) {
const response = await this.fetch(
`/projects/user/${encodeURIComponent(owner)}/${encodeURIComponent(name)}`,
);
return await response.json();
}

async getProjectMetadataByName(owner, name) {
const response = await this.fetch(
`/projects/user/${owner}/${name}/metadata`,
`/projects/user/${encodeURIComponent(owner)}/${
encodeURIComponent(name)
}/metadata`,
);
return await response.json();
}

async startNetworkTrace(projectId) {
async startNetworkTrace(projectId: string) {
const response = await this.post(`/network/id/${projectId}/trace/`);
return await response.text();
}
Expand Down
58 changes: 44 additions & 14 deletions test/client.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const { CloudClient } = require("..");
const assert = require("assert");
const adminUser = "admin";
const password = "somePassword";

const client = new CloudClient("http://localhost:7777");
const client = new CloudClient("http://127.0.0.1:7777");

describe("localize", function () {
it("should have localize fn (default: identity)", function () {
Expand All @@ -11,31 +13,31 @@ describe("localize", function () {

describe("login", function () {
it("should login on success", async function () {
const username = await client.login("test", "password");
assert.equal(username, "test");
const username = await client.login(adminUser, password);
assert.equal(username, adminUser);
});

it("should maintain state", async function () {
await client.login("test", "password");
await client.login(adminUser, password);
const { username } = await client.viewUser();
assert.equal(username, "test");
assert.equal(username, adminUser);
});

it("should throw error on bad password", async function () {
await assert.rejects(
client.login("test", "incorrectPassword"),
client.login(adminUser, "incorrectPassword"),
);
});

it("should throw error on invalid URL", async function () {
const client = new CloudClient("http://localhost:da7777");
await assert.rejects(client.login("test", "password"));
await assert.rejects(client.login(adminUser, password));
});
});

describe("logout", function () {
before(async () => {
await client.login("test", "password");
await client.login(adminUser, password);
});

it("should logout", async function () {
Expand All @@ -46,7 +48,7 @@ describe("logout", function () {

describe("saveRole", function () {
before(async () => {
await client.login("test", "password");
await client.login(adminUser, password);
await client.newProject();
});

Expand All @@ -66,7 +68,7 @@ describe("saveRole", function () {

describe("addRole", function () {
before(async () => {
await client.login("test", "password");
await client.login(adminUser, password);
await client.newProject();
});

Expand All @@ -79,16 +81,44 @@ describe("addRole", function () {
});
});

describe("getProjectMetadataByName", function () {
const name = "some name ";
before(async () => {
await client.login(adminUser, password);
await client.newProject();
await client.renameProject(name);
});

it("should support spaces in the name", async function () {
const metadata = await client.getProjectMetadataByName(adminUser, name);
assert.equal(metadata.name, name);
});
});

describe("getProjectByName", function () {
const name = "some name ";
before(async () => {
await client.login(adminUser, password);
await client.newProject();
await client.renameProject(name);
});

it("should support spaces in the name", async function () {
const metadata = await client.getProjectByName(adminUser, name);
assert.equal(metadata.name, name);
});
});

describe("register", function () {
it("should create new user", async function () {
const client = new CloudClient("http://localhost:7777");
const client = new CloudClient("http://127.0.0.1:7777");
await client.register("testuser", "testuser@netsblox.org");
});
});

describe("checkLogin", function () {
it("should report true when logged in", async function () {
const username = await client.login("test", "password");
const username = await client.login(adminUser, password);
assert(await client.checkLogin());
});

Expand All @@ -100,9 +130,9 @@ describe("checkLogin", function () {

describe("getProfile", function () {
it("should get the user profile", async function () {
const username = await client.login("test", "password");
const username = await client.login(adminUser, password);
const profile = await client.getProfile();
assert.equal(profile.username, "test");
assert.equal(profile.username, adminUser);
assert(profile.email);
});
});
37 changes: 0 additions & 37 deletions test/cloudConfig.toml

This file was deleted.

25 changes: 6 additions & 19 deletions test/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,19 @@ version: "3"
services:
cloud: # Basic account management, network overlay, etc
restart: always
image: netsblox/cloud:latest
volumes:
- ${PWD}/test/cloudConfig.toml:/config/default.toml
ports:
- "7777:7777"
image: netsblox/cloud:nightly
environment:
- "RUN_MODE=local"
network_mode: host
depends_on:
- mongo
- minio

browser: # Hosts the client files
restart: always
image: netsblox/browser:latest
ports:
- "8080:8080"
environment:
- "CLOUD_URL=http://127.0.0.1:7777"
- "PORT=8080"
# Uncomment the following 2 lines to use the local browser code in the container (for development)
volumes:
- "${PWD}/browser/src:/netsblox/src"
depends_on:
- cloud

mongo: # Database for users, groups, project metadata, etc
restart: always
image: mongo:5.0-focal
ports:
- "27017:27017"
volumes:
- mongodb_data:/data/db

Expand Down

0 comments on commit d012593

Please sign in to comment.