diff --git a/package-lock.json b/package-lock.json index 543b0ca..a441f50 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "netsblox-cloud-client", - "version": "2.0.0", + "version": "2.1.0", "lockfileVersion": 2, "requires": true, "packages": { diff --git a/package.json b/package.json index 2a24c19..d6646cb 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/client.ts b/src/client.ts index 8deffca..0df5d28 100644 --- a/src/client.ts +++ b/src/client.ts @@ -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, @@ -209,12 +209,13 @@ export default class Cloud { //return await response.json(); } - async renameProject(name) { + async renameProject(name: string): Promise { const body = { name, clientId: this.clientId, }; const response = await this.patch(`/projects/id/${this.projectId}`, body); + return await response.json(); } async reportLatestRole(id, data) { @@ -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(); } diff --git a/test/client.spec.js b/test/client.spec.js index 46da405..56a9c3d 100644 --- a/test/client.spec.js +++ b/test/client.spec.js @@ -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 () { @@ -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 () { @@ -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(); }); @@ -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(); }); @@ -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()); }); @@ -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); }); }); diff --git a/test/cloudConfig.toml b/test/cloudConfig.toml deleted file mode 100644 index cf648c5..0000000 --- a/test/cloudConfig.toml +++ /dev/null @@ -1,37 +0,0 @@ -address = "0.0.0.0:7777" -public_url = "http://127.0.0.1:7777" -#login_url = "https://login.netsblox.org" - -[admin] -username = "test" -password = "password" -email = "test@netsblox.org" - -[database] -url = "mongodb://mongo:27017/" -name = "netsblox-rs" - -[s3] -bucket = "netsbloxrs" -endpoint = "http://minio:9000" -region_name = "" - -[s3.credentials] -access_key = "KEY" -secret_key = "MYSECRET" - -[cookie] -name = "netsblox" -domain = "localhost" -key = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789ab" - -[email] -sender = "NetsBlox " - -[email.smtp] -host = "smtp.gmail.com" -username = "USERNAME" -password = "PASSWORD" - -[security] -allow_tor_login = false diff --git a/test/docker-compose.yml b/test/docker-compose.yml index 0db9293..9e63ed8 100644 --- a/test/docker-compose.yml +++ b/test/docker-compose.yml @@ -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