From c3a67601dd68910f5f13cce7a9f4193469c24d8e Mon Sep 17 00:00:00 2001 From: Vahid Al Date: Fri, 31 May 2024 23:47:45 +0330 Subject: [PATCH] Deprecate transaction endpoint --- .env.sample | 2 +- README.md | 8 +++- docs/api/root-examples.md | 40 ------------------ package-lock.json | 4 +- package.json | 2 +- src/constants/messages.js | 3 +- src/controllers/auth.test.js | 2 +- src/controllers/index.js | 78 ++--------------------------------- src/controllers/index.test.js | 27 ------------ src/routes/index.js | 7 ---- src/swagger/index.js | 3 +- src/swagger/swagger.json | 27 +----------- 12 files changed, 20 insertions(+), 183 deletions(-) diff --git a/.env.sample b/.env.sample index c98fc7e..563f702 100644 --- a/.env.sample +++ b/.env.sample @@ -15,7 +15,7 @@ DB=foobar.db ACCESS_TOKEN_EXPIRATION_TIME=10H REFRESH_TOKEN_EXPIRATION_TIME=2D -INITIAL_USER_USERNAME +INITIAL_USER_USERNAME INITIAL_USER_PASSWORD TOKEN_SECRET diff --git a/README.md b/README.md index 3ce11f6..57e8cd2 100644 --- a/README.md +++ b/README.md @@ -157,12 +157,18 @@ npm run dev # Start the dev server ## Testing -Set the `AUTH` variable to `true` in your `.env` file and use the command below to run the tests +1. Set the `AUTH` variable to true in your `.env` file. +2. Provide a username for the `INITIAL_USER_USERNAME` environment variable. The username should be a valid, meaningful username. +3. Provide a strong password for the `INITIAL_USER_PASSWORD` environment variable. The password should be at least 8 characters long and contain a combination of lowercase letters, uppercase letters, numbers, and special characters, for example: "Str0ng$Pw!". +4. Provider a secret for the `TOKEN_SECRET` environment variable. +5. Use the following command to run the tests: ``` npm run test ``` +Make sure to replace the placeholders with the appropriate values for your environment. + ## Community [Join](https://bit.ly/soul-discord) the discussion in our Discord server and help making Soul together. diff --git a/docs/api/root-examples.md b/docs/api/root-examples.md index 49415f4..4bcfc6a 100644 --- a/docs/api/root-examples.md +++ b/docs/api/root-examples.md @@ -1,41 +1 @@ ## Root - -### 1. Transaction - -To start a transaction call `/transaction` endpoint with `POST` method. - -```bash -curl --request POST \ - --url http://localhost:8000/api/transaction \ - --header 'Content-Type: application/json' \ - --data '{ - "transaction": [ - { - "statement": "INSERT INTO Artist (ArtistId, Name) VALUES (:id, :name)", - "values": { "id": 100000, "name": "Glen Hansard" } - }, - { - "query": "SELECT * FROM Artist ORDER BY ArtistId DESC LIMIT 1" - } - ] -}' -``` - -Response - -```json -{ - "data": [ - { - "changes": 1, - "lastInsertRowid": 100000 - }, - [ - { - "ArtistId": 100000, - "Name": "Glen Hansard" - } - ] - ] -} -``` \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 2bb8b92..3ca112d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "soul-cli", - "version": "0.7.9", + "version": "0.8.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "soul-cli", - "version": "0.7.9", + "version": "0.8.0", "license": "MIT", "dependencies": { "bcrypt": "^5.1.1", diff --git a/package.json b/package.json index 1dfa3c3..7affc70 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "soul-cli", - "version": "0.7.9", + "version": "0.8.0", "description": "A SQLite REST and Realtime server", "main": "src/server.js", "bin": { diff --git a/src/constants/messages.js b/src/constants/messages.js index 5755406..67d108c 100644 --- a/src/constants/messages.js +++ b/src/constants/messages.js @@ -10,7 +10,8 @@ module.exports = { errorMessage: { USERNAME_TAKEN_ERROR: 'This username is taken', - WEAK_PASSWORD_ERROR: 'This password is weak, please use another password', + WEAK_PASSWORD_ERROR: + 'This password is weak, it should be at least 8 characters long and contain a combination of lowercase letters, uppercase letters, numbers, and special characters', DEFAULT_ROLE_NOT_CREATED_ERROR: 'Please restart soul so a default role can be created', INVALID_USERNAME_PASSWORD_ERROR: 'Invalid username or password', diff --git a/src/controllers/auth.test.js b/src/controllers/auth.test.js index ec38345..79965d9 100644 --- a/src/controllers/auth.test.js +++ b/src/controllers/auth.test.js @@ -78,7 +78,7 @@ describe('Auth Endpoints', () => { expect(res.status).toEqual(400); expect(res.body.message).toBe( - 'This password is weak, please use another password', + 'This password is weak, it should be at least 8 characters long and contain a combination of lowercase letters, uppercase letters, numbers, and special characters', ); expect(res.body).not.toHaveProperty('password'); diff --git a/src/controllers/index.js b/src/controllers/index.js index 20fe7af..7f9cabb 100644 --- a/src/controllers/index.js +++ b/src/controllers/index.js @@ -1,12 +1,11 @@ -const db = require('../db/index'); const version = require('../../package.json').version; // Root endpoint const root = async (req, res) => { - /* + /* #swagger.tags = ['Root'] - #swagger.summary = 'Timestamp' - #swagger.description = 'Endpoint to return server timestamp' + #swagger.summary = 'Timestamp' + #swagger.description = 'Endpoint to return server timestamp' */ res.json({ @@ -18,77 +17,6 @@ const root = async (req, res) => { }); }; -// Run any query transactions -// inspired by https://github.com/proofrock/ws4sqlite -// e.g. body: -// "transaction": [ -// { -// "statement": "INSERT INTO users (id, firstName, lastName) VALUES (:id, :firstName, :lastName)", -// "values": { "id": 1, "firstName": "John", "lastName": "Doe" } -// }, -// { -// "query": "SELECT * FROM users" -// } -// } -// -// response: -// "data": [ -// { -// "changes": 1, -// "lastInsertRowid": 1 -// }, -// [ -// { -// "id": 1, -// "createdAt": "2022-10-10 10:55:29", -// "updatedAt": "2022-10-10 10:55:29", -// "firstName": "John", -// "lastName": "Doe" -// } -// ] -// ] -// - -const transaction = async (req, res) => { - /* - #swagger.tags = ['Root'] - #swagger.summary = 'Transaction' - #swagger.description = 'Endpoint to run any transaction, e.g. [{ "query": "" }, { "statement": "", "values": {} }, { "query": "" }]', - #swagger.parameters['body'] = { - in: 'body', - required: true, - schema: { $ref: "#/definitions/TransactionRequestBody" } - } - */ - const { transaction } = req.body; - const results = []; - try { - db.transaction(() => { - transaction.forEach((query) => { - if (query.statement) { - const { statement, values } = query; - const data = db.prepare(statement).run(values); - results.push(data); - } else if (query.query) { - const { query: queryString } = query; - const data = db.prepare(queryString).all(); - results.push(data); - } - }); - })(); - - res.json({ - data: results, - }); - } catch (error) { - res.status(400).json({ - message: error.message, - error: error, - }); - } -}; - module.exports = { root, - transaction, }; diff --git a/src/controllers/index.test.js b/src/controllers/index.test.js index 54011c5..448f988 100644 --- a/src/controllers/index.test.js +++ b/src/controllers/index.test.js @@ -14,30 +14,3 @@ describe('Root Endpoints', () => { expect(res.body.data).toHaveProperty('timestamp'); }); }); - -describe('Transaction Endpoint', () => { - it('POST /transaction should commit transaction and return an array of changes and lastInsertRowid', async () => { - const res = await requestWithSupertest.post('/api/transaction').send({ - transaction: [ - { - statement: `CREATE TABLE students (id INTEGER PRIMARY KEY, firstName TEXT, lastName TEXT)`, - values: {}, - }, - { - statement: `INSERT INTO students (id, firstName, lastName) VALUES (:id, :firstName, :lastName)`, - values: { id: 1, firstName: 'John', lastName: 'Doe' }, - }, - { - query: `SELECT * FROM students`, - }, - ], - }); - - expect(res.status).toEqual(200); - expect(res.type).toEqual(expect.stringContaining('json')); - expect(res.body).toHaveProperty('data'); - expect(res.body.data).toEqual(expect.any(Array)); - expect(res.body.data[0]).toHaveProperty('changes'); - expect(res.body.data[0]).toHaveProperty('lastInsertRowid'); - }); -}); diff --git a/src/routes/index.js b/src/routes/index.js index 74cb310..a8b0de3 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -1,16 +1,9 @@ const express = require('express'); const controllers = require('../controllers/index'); -const { validator } = require('../middlewares/validation'); -const schema = require('../schemas/index'); const router = express.Router(); router.get('/', controllers.root); -router.post( - '/transaction', - validator(schema.transaction), - controllers.transaction -); module.exports = router; diff --git a/src/swagger/index.js b/src/swagger/index.js index e606d1f..b739bc9 100644 --- a/src/swagger/index.js +++ b/src/swagger/index.js @@ -152,7 +152,8 @@ const doc = { }, WeakPasswordErrorResponse: { - message: 'This password is weak, please use another password', + message: + 'This password is weak, it should be at least 8 characters long and contain a combination of lowercase letters, uppercase letters, numbers, and special characters', }, UsernameTakenErrorResponse: { diff --git a/src/swagger/swagger.json b/src/swagger/swagger.json index b3f2f2a..8a560c6 100644 --- a/src/swagger/swagger.json +++ b/src/swagger/swagger.json @@ -1,7 +1,7 @@ { "swagger": "2.0", "info": { - "version": "0.7.2", + "version": "0.8.0", "title": "Soul API", "description": "API Documentation for Soul, a SQLite REST and realtime server. " }, @@ -54,31 +54,6 @@ } } }, - "/api/transaction": { - "post": { - "tags": ["Root"], - "summary": "Transaction", - "description": "Endpoint to run any transaction, e.g. [{ \"query\": \"\" }, { \"statement\": \"\", \"values\": {} }, { \"query\": \"\" }]", - "parameters": [ - { - "name": "body", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/TransactionRequestBody" - } - } - ], - "responses": { - "200": { - "description": "OK" - }, - "400": { - "description": "Bad Request" - } - } - } - }, "/api/tables/": { "get": { "tags": ["Tables"],