Skip to content
Merged
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
8 changes: 1 addition & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,14 @@
"serve": "node dist/server.js",
"lambda-deploy-dev": "yarn build && yarn claudia create --config claudia.dev.json --region us-east-2 --handler lambda.handler --deploy-proxy-api --no-optional-dependencies --set-env-from-json .env.prod.json",
"lambda-update-dev": "yarn build && yarn claudia update --set-env-from-json .env.prod.json",

"lambda-deploy-testnet-use1": "yarn build && yarn claudia create --config claudia.use1.testnet.json --name 'storage-node-use1-testnet' --region us-east-1 --role arn:aws:iam::737954963756:role/verida_storage-node-executor --handler lambda.handler --deploy-proxy-api --no-optional-dependencies --set-env-from-json .env.use1.testnet.json",
"lambda-update-testnet-use1": "yarn build && yarn claudia update --config claudia.use1.testnet.json --set-env-from-json .env.use1.testnet.json",

"lambda-deploy-testnet-sg1": "yarn build && yarn claudia create --config claudia.sg1.testnet.json --name 'storage-node-sg1-testnet' --region ap-southeast-1 --role arn:aws:iam::737954963756:role/verida_storage-node-executor --handler lambda.handler --deploy-proxy-api --no-optional-dependencies --set-env-from-json .env.sg1.testnet.json",
"lambda-update-testnet-sg1": "yarn build && yarn claudia update --config claudia.sg1.testnet.json --set-env-from-json .env.sg1.testnet.json",

"lambda-deploy-testnet-au1": "yarn build && yarn claudia create --config claudia.au1.testnet.json --name 'storage-node-au1-testnet' --region ap-southeast-2 --role arn:aws:iam::737954963756:role/verida_storage-node-executor --handler lambda.handler --deploy-proxy-api --no-optional-dependencies --set-env-from-json .env.au1.testnet.json",
"lambda-update-testnet-au1": "yarn build && yarn claudia update --config claudia.au1.testnet.json --set-env-from-json .env.au1.testnet.json",

"lambda-deploy-testnet-acacia": "yarn build && yarn claudia create --config claudia.acacia.testnet.json --name 'storage-node-acacia-testnet' --region ap-southeast-2 --role arn:aws:iam::737954963756:role/verida_storage-node-executor --handler lambda.handler --deploy-proxy-api --no-optional-dependencies --set-env-from-json .env.acacia.testnet.json",
"lambda-update-testnet-acacia": "yarn build && yarn claudia update --config claudia.acacia.testnet.json --set-env-from-json .env.acacia.testnet.json",



"lambda-pack": "yarn build && yarn claudia pack --no-optional-dependencies"
},
"files": [
Expand Down Expand Up @@ -81,6 +74,7 @@
"@babel/polyfill": "^7.8.0",
"@babel/preset-env": "^7.7.7",
"@verida/account-node": "^1.1.9",
"@verida/client-ts": "^2.0.0-rc1",
"axios": "^0.27.2",
"babel-core": "^6.26.3",
"babel-loader": "^8.2.3",
Expand Down
39 changes: 39 additions & 0 deletions src/controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,45 @@ class UserController {
}
}

async deleteDatabases(req, res) {
const did = req.tokenData.did
const contextName = req.tokenData.contextName
const username = req.tokenData.username

if (!did || !contextName) {
return res.status(401).send({
status: "fail",
message: "Permission denied"
});
}

const databases = await DbManager.getUserDatabases(did, contextName)
const results = []

for (let d in databases) {
const database = databases[d]
const databaseHash = Utils.generateDatabaseName(did, contextName, database.databaseName)
try {
let success = await DbManager.deleteDatabase(databaseHash, username);
if (success) {
await DbManager.deleteUserDatabase(did, contextName, database.databaseName, databaseHash)
results.push(database.databaseName)
}
} catch (err) {
return res.status(500).send({
status: "fail",
message: err.error + ": " + err.reason,
results
});
}
};

return res.status(200).send({
status: "success",
results
});
}

// Update permissions on a user's database
// @todo: database name should be in plain text, then hashed
async updateDatabase(req, res) {
Expand Down
1 change: 1 addition & 0 deletions src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const router = express.Router();
router.post('/user/createDatabase', UserController.createDatabase);
router.post('/user/updateDatabase', UserController.updateDatabase);
router.post('/user/deleteDatabase', UserController.deleteDatabase);
router.post('/user/deleteDatabases', UserController.deleteDatabases);
router.post('/user/databases', UserController.databases);
router.post('/user/databaseInfo', UserController.databaseInfo);

Expand Down
9 changes: 7 additions & 2 deletions test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ import { EnvironmentType } from "@verida/account"
require('dotenv').config();

export default {
DID_SERVER_URL: undefined,// 'http://localhost:5001',
DID_CLIENT_CONFIG: {
networkPrivateKey: '',
callType: 'web3',
web3Config: {},
rpcUrl: '
},
ENVIRONMENT: EnvironmentType.TESTNET,
SERVER_URL: `http://localhost:${process.env.PORT}`,
VDA_PRIVATE_KEY: '0x09d3b996ec98a9a536efdffbae40e5eaaf117765a587483c69195c9460165c31',
VDA_PRIVATE_KEY: '0x19d3b996ec98a9a536efdffbae41e5eaaf117765a587483c69195c9460165c33',
CONTEXT_NAME: 'Verida Storage Node Test: Test Application 1',
DATABASE_SERVER: 'https://db.testnet.verida.io:5002/', // http://localhost:5000/ for local testing when running local @verida/storage-node
MESSAGE_SERVER: 'https://db.testnet.verida.io:5002/', // http://localhost:5000/ for local testing when running local @verida/storage-node
Expand Down
42 changes: 30 additions & 12 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const jwt = require('jsonwebtoken');
import Axios from 'axios'

import AuthManager from "../src/components/authManager";
import UserManager from "../src/components/userManager";
import TestUtils from "./utils"

import CONFIG from './config'
Expand All @@ -14,9 +13,12 @@ const { CONTEXT_NAME, SERVER_URL, TEST_DEVICE_ID } = CONFIG
let authJwt, accountInfo, authRequestId
let refreshToken, accessToken, newRefreshToken

// @todo: Generate Verida account with private key so DID document exists and storage node can then verify signature!

describe("Server tests", function() {
this.beforeAll(async () => {
await AuthManager.initDb()
await TestUtils.ensureVeridaAccount(CONFIG.VDA_PRIVATE_KEY)
accountInfo = await TestUtils.connectAccount(CONFIG.VDA_PRIVATE_KEY)
})

Expand All @@ -43,7 +45,7 @@ describe("Server tests", function() {
contextName: CONTEXT_NAME,
signature,
deviceId: TEST_DEVICE_ID
});
})

assert.ok(authenticateResponse && authenticateResponse.data && authenticateResponse.data.refreshToken, "Have refreshToken in response")
assert.equal(authenticateResponse.data.status, 'success', "Success response")
Expand Down Expand Up @@ -168,16 +170,7 @@ describe("Server tests", function() {
const databaseName2 = "helloooo2"

it("Creates database", async () => {
const response = await Axios.post(`${SERVER_URL}/user/createDatabase`, {
databaseName,
did: accountInfo.did,
contextName: CONTEXT_NAME
}, {
headers: {
Authorization: `Bearer ${accessToken}`
}
});

const response = await TestUtils.createDatabase(databaseName, accountInfo.did, CONTEXT_NAME, accessToken)
assert.equal(response.data.status, "success", "Successful create response")
})

Expand Down Expand Up @@ -267,6 +260,31 @@ describe("Server tests", function() {
});
})

it("Deletes all database", async () => {
const db1 = await TestUtils.createDatabase('DeleteAll_1', accountInfo.did, CONTEXT_NAME, accessToken)
const db2 = await TestUtils.createDatabase('DeleteAll_2', accountInfo.did, CONTEXT_NAME, accessToken)
const db3 = await TestUtils.createDatabase('DeleteAll_3', accountInfo.did, CONTEXT_NAME, accessToken)

assert.equal(db1.data.status, "success", "Successful create response for db1")
assert.equal(db2.data.status, "success", "Successful create response for db2")
assert.equal(db3.data.status, "success", "Successful create response for db3")

const response = await Axios.post(`${SERVER_URL}/user/deleteDatabases`, {
did: accountInfo.did,
contextName: CONTEXT_NAME
}, {
headers: {
Authorization: `Bearer ${accessToken}`
}
});

assert.equal(response.data.status, "success", "Successful delete response")
assert.equal(response.data.results.length, 3, "Deleted three databases")
assert.ok(response.data.results.indexOf('DeleteAll_1') >= 0, 'Deleted correct databases (DeleteAll_1)')
assert.ok(response.data.results.indexOf('DeleteAll_2') >= 0, 'Deleted correct databases (DeleteAll_2)')
assert.ok(response.data.results.indexOf('DeleteAll_3') >= 0, 'Deleted correct databases (DeleteAll_3)')
})


})
})
36 changes: 35 additions & 1 deletion test/utils.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
import Axios from 'axios'
const PouchDb = require('pouchdb');
import { AutoAccount } from "@verida/account-node"
import { Network } from "@verida/client-ts"

import CONFIG from './config'

class Utils {

async ensureVeridaAccount(privateKey) {
const account = new AutoAccount(CONFIG.DEFAULT_ENDPOINTS, {
privateKey: privateKey,
didClientConfig: CONFIG.DID_CLIENT_CONFIG,
environment: CONFIG.ENVIRONMENT
})

await Network.connect({
client: {
environment: 'testnet'
},
account: account,
context: {
name: CONFIG.CONTEXT_NAME
}
})
}

async connectAccount(privateKey) {
const account = new AutoAccount(CONFIG.DEFAULT_ENDPOINTS, {
privateKey: privateKey,
didServerUrl: CONFIG.DID_SERVER_URL,
didClientConfig: CONFIG.DID_CLIENT_CONFIG,
environment: CONFIG.ENVIRONMENT
})

Expand All @@ -32,6 +52,20 @@ class Utils {
});
}

async createDatabase(databaseName, did, contextName, accessToken) {
const response = await Axios.post(`${CONFIG.SERVER_URL}/user/createDatabase`, {
databaseName,
did,
contextName
}, {
headers: {
Authorization: `Bearer ${accessToken}`
}
});

return response
}

}

const utils = new Utils()
Expand Down
Loading