Skip to content

Commit

Permalink
merge /api/v1/** and /scenes/** routes
Browse files Browse the repository at this point in the history
  • Loading branch information
sdumetz committed Jul 8, 2024
1 parent efb9670 commit c5e0f03
Show file tree
Hide file tree
Showing 65 changed files with 568 additions and 501 deletions.
2 changes: 1 addition & 1 deletion source/server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
As long as no user exist, the application launches in "open" mode. An unauthenticated request can be used to create the first user :

```
curl -XPOST -H "Content-Type: application/json" -d '{"username":"<...>", "password":"<...>", "email":"<...>", "isAdministrator": true}' "http://<hostname>:<port>/api/v1/users"
curl -XPOST -H "Content-Type: application/json" -d '{"username":"<...>", "password":"<...>", "email":"<...>", "isAdministrator": true}' "http://<hostname>:<port>/users"
```

Then restart the application to enable permissions management
Expand Down
5 changes: 5 additions & 0 deletions source/server/__test_fixtures/fixtures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import path from "path";
import { fileURLToPath } from 'url';


export const fixturesDir = path.dirname(fileURLToPath(import.meta.url));
2 changes: 1 addition & 1 deletion source/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

import path from "path";
import createServer from "./server.js";
import createServer from "./routes/index.js";
import config from "./utils/config.js";

//@ts-ignore
Expand Down
34 changes: 16 additions & 18 deletions source/server/integration.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import fs from "fs/promises";
import path from "path";
import { fileURLToPath } from 'url';
import {tmpdir} from "os";

import request from "supertest";
import createServer from "./server.js";
import Vfs, { DocProps, WriteFileParams } from "./vfs/index.js";
import Vfs from "./vfs/index.js";
import User from "./auth/User.js";
import { Element, xml2js } from "xml-js";
import UserManager from "./auth/UserManager.js";

const thisDir = path.dirname(fileURLToPath(import.meta.url));
import { fixturesDir } from "./__test_fixtures/fixtures.js";



describe("Web Server Integration", function(){
let vfs :Vfs, userManager :UserManager, user :User, admin :User;
Expand Down Expand Up @@ -56,24 +54,24 @@ describe("Web Server Integration", function(){
await request(this.server).put("/scenes/foo/models/bar.glb").expect(401);
});
it("can't fetch user list", async function(){
await request(this.server).get("/api/v1/users")
await request(this.server).get("/users")
.expect(401);
});
});

describe("(author)", function(){
this.beforeEach(async function(){
this.agent = request.agent(this.server);
await this.agent.post("/api/v1/login")
await this.agent.post("/auth/login")
.send({username: user.username, password: "12345678"})
.set("Content-Type", "application/json")
.set("Accept", "")
.expect(200);
});

it("can create a new scene", async function(){
let content = await fs.readFile(path.join(thisDir, "__test_fixtures/cube.glb"));
let r = await this.agent.post("/api/v1/scenes/bar")
let content = await fs.readFile(path.join(fixturesDir, "cube.glb"));
let r = await this.agent.post("/scenes/bar")
.set("Content-Type", "application/octet-stream")
.send(content)
.expect(201);
Expand All @@ -86,7 +84,7 @@ describe("Web Server Integration", function(){
});

it("can upload a glb model in an existing scene", async function(){
let content = await fs.readFile(path.join(thisDir, "__test_fixtures/cube.glb"));
let content = await fs.readFile(path.join(fixturesDir, "cube.glb"));
await this.agent.put("/scenes/foo/models/baz.glb")
.send(content)
.expect(201);
Expand Down Expand Up @@ -137,11 +135,11 @@ describe("Web Server Integration", function(){

it("can grant permissions", async function(){
let dave = await userManager.addUser("dave", "12345678");
await this.agent.patch("/api/v1/scenes/foo/permissions")
await this.agent.patch("/auth/access/foo")
.send({username: "dave", access: "write"})
.expect(204);

let r = await this.agent.get("/api/v1/scenes/foo/permissions")
let r = await this.agent.get("/auth/access/foo")
.expect(200)
.expect("Content-Type", "application/json; charset=utf-8");
expect(r, JSON.stringify(r.body)).to.have.property("body").to.deep.equal([
Expand All @@ -153,11 +151,11 @@ describe("Web Server Integration", function(){
});

it("can make a model private", async function(){
await this.agent.patch("/api/v1/scenes/foo/permissions")
await this.agent.patch("/auth/access/foo")
.send({username: "default", access: "none"})
.expect(204);

let r = await this.agent.get("/api/v1/scenes/foo/permissions")
let r = await this.agent.get("/auth/access/foo")
.expect(200)
.expect("Content-Type", "application/json; charset=utf-8");
expect(r).to.have.property("body").to.deep.equal([
Expand All @@ -168,11 +166,11 @@ describe("Web Server Integration", function(){
});

it("can remove a user's special permissions", async function(){
await this.agent.patch("/api/v1/scenes/foo/permissions")
await this.agent.patch("/auth/access/foo")
.send({username: user.username, access: null})
.expect(204);

let r = await this.agent.get("/api/v1/scenes/foo/permissions")
let r = await this.agent.get("/auth/access/foo")
.expect(200)
.expect("Content-Type", "application/json; charset=utf-8");
expect(r).to.have.property("body").to.deep.equal([
Expand All @@ -189,7 +187,7 @@ describe("Web Server Integration", function(){
eve = await userManager.addUser("eve", "12345678");

this.agent = request.agent(this.server);
await this.agent.post("/api/v1/login")
await this.agent.post("/auth/login")
.send({username: eve.username, password: "12345678"})
.set("Content-Type", "application/json")
.set("Accept", "")
Expand Down
26 changes: 26 additions & 0 deletions source/server/routes/admin/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

import { Router } from "express";

import { isAdministrator } from "../../utils/locals.js";
import wrap from "../../utils/wrapAsync.js";
import handleGetStats from "./stats/index.js";
import handleMailtest from "./mailtest.js";


const router = Router();

/** Configure cache behaviour for the whole API
* Settings can be changed individually further down the line
*/
router.use((req, res, next)=>{
//Browser should always make the request
res.set("Cache-Control", "no-cache");
next();
});



router.get("/admin/stats", isAdministrator, wrap(handleGetStats));
router.post("/admin/mailtest", isAdministrator, wrap(handleMailtest));

export default router;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Request, Response } from "express";
import sendmail from "../../../../utils/mails/send.js";
import { getLocals, getUser } from "../../../../utils/locals.js";
import { BadRequestError } from "../../../../utils/errors.js";
import sendmail from "../../utils/mails/send.js";
import { getLocals, getUser } from "../../utils/locals.js";
import { BadRequestError } from "../../utils/errors.js";

/**
* Send a test email
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Request, Response } from "express";
import { getVfs } from "../../../../../utils/locals.js";
import { getVfs } from "../../../utils/locals.js";



Expand Down
90 changes: 0 additions & 90 deletions source/server/routes/api/v1/index.ts

This file was deleted.

19 changes: 0 additions & 19 deletions source/server/routes/api/v1/scenes/scene/files/get.ts

This file was deleted.

58 changes: 1 addition & 57 deletions source/server/routes/apipaths.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,64 +5,8 @@ info:
servers:
- url: https://ecorpus.holusion.com
paths:
/admin/stats:
get:
description: get server stats
/admin/mailtest:
post:
description: sends a test email
/login:
get:
description: get login data
post:
description: log-in to the server
/login/{username}/link:
get:
description: get a login link for this user
post:
description: generate and send a login link for this user
/logout:
post:
description: delete this request's credentials
/users:
get:
description: get a list of registered users
post:
description: create a new user
/users/{uid}:
delete:
description: delete a user
patch:
description: change a user's data

/scenes:
get:
description: get a list of scenes with optional search parameters
post:
description: import an archive of scenes
/scenes/{scene}:
get:
description: get a scene's metadata
post:
description: creates a new scene using attached data
patch:
description: Edit scene's metadata
/scenes/{scene}/history:
get:
description: get a full history of a scene's modifications
post:
description: edit a scene's history
/scenes/{scene}/files:
get:
description: list all files in the scenes in their current state
/scenes/{scene}/permissions:
/scenes/{scene}/permissions:
get:
description: get scene permissions map
patch:
description: edit scene permission map
/tags:
get:
description: get a list of tags on this server
/tags/{tag}:
get:
description: get all scenes associated with this tag
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { randomBytes } from "crypto";
import request from "supertest";
import Vfs from "../../../../../../vfs/index.js";
import User from "../../../../../../auth/User.js";
import UserManager from "../../../../../../auth/UserManager.js";
import User from "../../../auth/User.js";
import UserManager from "../../../auth/UserManager.js";
import Vfs from "../../../vfs/index.js";



/**
* Minimal tests as most
*/

describe("GET /api/v1/scenes/:scene/permissions", function(){
describe("GET /auth/access/:scene", function(){
let vfs :Vfs, userManager :UserManager, user :User, admin :User, opponent :User;

let titleSlug :string, scene_id :number;
Expand All @@ -37,11 +37,11 @@ describe("GET /api/v1/scenes/:scene/permissions", function(){
await userManager.grant(titleSlug, "default", "none");
await userManager.grant(titleSlug, "any", "none");
//Anonymous
await request(this.server).get(`/api/v1/scenes/${titleSlug}/permissions`)
await request(this.server).get(`/auth/access/${titleSlug}`)
.expect(404);
//read-only User
await request(this.server).get(`/api/v1/scenes/${titleSlug}/permissions`)
await request(this.server).get(`/auth/access/${titleSlug}`)
.auth(opponent.username, "12345678")
.expect(404);
});
});
});
Loading

0 comments on commit c5e0f03

Please sign in to comment.