Tested live JSON RPC

This commit is contained in:
Chris Troutner
2021-04-07 18:42:27 -07:00
parent 1bae340aae
commit 1877873892
3 changed files with 81 additions and 50 deletions
+4 -2
View File
@@ -2,5 +2,7 @@
Below are a series of JSON RPC calls that can be manually entered at chat.fullstack.cash to interact with the JSON RPC of this IPFS Service Provider.
- `{"jsonrpc":"2.0","id":"123","method":"users","params":{ "endpoint": "getAll"}}`
- `{"jsonrpc":"2.0","id":"457","method":"auth","params":{ "endpoint": "authUser", "login": "test@test.com", "password": "password"}}`
- `{"jsonrpc":"2.0","id":"555","method":"users","params":{ "endpoint": "createUser", "email": "test555@test.com", "name": "testy tester", "password": "password"}}`
- `{"jsonrpc":"2.0","id":"556","method":"auth","params":{ "endpoint": "authUser", "login": "test555@test.com", "password": "password"}}`
- `{"jsonrpc":"2.0","id":"123","method":"users","params":{ "endpoint": "getAllUsers", "apiToken": "<JWT>"}}`
- `{"jsonrpc":"2.0","id":"123","method":"users","params":{ "endpoint": "deleteUser", "userId": "<_id>", "apiToken": "<JWT>"}}`
+11 -8
View File
@@ -22,7 +22,7 @@ class UserRPC {
// methods.
async userRouter (rpcData) {
try {
console.log('userRouter rpcData: ', rpcData)
// console.log('userRouter rpcData: ', rpcData)
const endpoint = rpcData.payload.params.endpoint
let user
@@ -31,14 +31,17 @@ class UserRPC {
switch (endpoint) {
case 'createUser':
return await this.createUser(rpcData)
case 'getAllUsers':
await this.validators.ensureUser(rpcData)
return await this.getAll(rpcData)
case 'getUser':
return await this.getUser(rpcData)
case 'deleteUser':
user = await this.validators.ensureTargetUserOrAdmin(rpcData)
return await this.deleteUser(rpcData, user)
case 'getAllUsers':
// await this.validators.ensureUser(rpcData)
return await this.getAll(rpcData)
case 'getUser':
return await this.getUser(rpcData)
}
} catch (err) {
console.error('Error in UsersRPC/rpcRouter()')
@@ -49,7 +52,7 @@ class UserRPC {
// Create a new user
async createUser (rpcData) {
try {
console.log('createUser rpcData: ', rpcData)
// console.log('createUser rpcData: ', rpcData)
const retObj = await this.userLib.createUser(rpcData.payload.params)
@@ -79,7 +82,7 @@ class UserRPC {
async deleteUser (rpcData, userModel) {
try {
console.log('deleteUser rpcData: ', rpcData)
// console.log('deleteUser rpcData: ', rpcData)
await this.userLib.deleteUser(userModel)
+66 -40
View File
@@ -20,7 +20,7 @@ const UserModel = require('../../../src/models/users')
describe('#UserRPC', () => {
let uut
let sandbox
let testUserId
let testUser
before(async () => {
// Connect to the Mongo Database.
@@ -48,40 +48,6 @@ describe('#UserRPC', () => {
mongoose.connection.close()
})
describe('#userRouter', () => {
it('should route to the createUser method', async () => {
// Mock dependencies
sandbox.stub(uut, 'createUser').resolves(true)
// Generate the parsed data that the main router would pass to this
// endpoint.
const id = uid()
const userCall = jsonrpc.request(id, 'users', { endpoint: 'createUser' })
const jsonStr = JSON.stringify(userCall, null, 2)
const rpcData = jsonrpc.parse(jsonStr)
const result = await uut.userRouter(rpcData)
assert.equal(result, true)
})
it('should route to the getAllUsers method', async () => {
// Mock dependencies
sandbox.stub(uut, 'getAll').resolves(true)
// Generate the parsed data that the main router would pass to this
// endpoint.
const id = uid()
const userCall = jsonrpc.request(id, 'users', { endpoint: 'getAllUsers' })
const jsonStr = JSON.stringify(userCall, null, 2)
const rpcData = jsonrpc.parse(jsonStr)
const result = await uut.userRouter(rpcData)
assert.equal(result, true)
})
})
describe('#createUser', () => {
it('should create a new user', async () => {
// Generate the parsed data that the main router would pass to this
@@ -107,14 +73,72 @@ describe('#UserRPC', () => {
assert.property(result, 'token')
// Save the user ID for future tests.
testUserId = result.userData._id
testUser = result
})
})
describe('#userRouter', () => {
it('should route to the createUser method', async () => {
// Mock dependencies
sandbox.stub(uut, 'createUser').resolves(true)
// Generate the parsed data that the main router would pass to this
// endpoint.
const id = uid()
const userCall = jsonrpc.request(id, 'users', { endpoint: 'createUser' })
const jsonStr = JSON.stringify(userCall, null, 2)
const rpcData = jsonrpc.parse(jsonStr)
const result = await uut.userRouter(rpcData)
assert.equal(result, true)
})
it('should route to the getAllUsers method', async () => {
// Mock dependencies
sandbox.stub(uut, 'getAll').resolves(true)
// Generate the parsed data that the main router would pass to this
// endpoint.
const id = uid()
const userCall = jsonrpc.request(id, 'users', {
endpoint: 'getAllUsers',
apiToken: testUser.token
})
const jsonStr = JSON.stringify(userCall, null, 2)
const rpcData = jsonrpc.parse(jsonStr)
const result = await uut.userRouter(rpcData)
assert.equal(result, true)
})
it('should route to the deleteUsers method', async () => {
// Mock dependencies
sandbox.stub(uut, 'deleteUser').resolves(true)
// Generate the parsed data that the main router would pass to this
// endpoint.
const id = uid()
const userCall = jsonrpc.request(id, 'users', {
endpoint: 'deleteUser',
apiToken: testUser.token,
userId: testUser.userData._id
})
const jsonStr = JSON.stringify(userCall, null, 2)
const rpcData = jsonrpc.parse(jsonStr)
const result = await uut.userRouter(rpcData)
// console.log('result: ', result)
assert.equal(result, true)
})
})
describe('#getAllUsers', () => {
it('should return all users', async () => {
const result = await uut.getAll()
console.log('result: ', result)
// console.log('result: ', result)
assert.equal(result.endpoint, 'getAllUsers')
assert.property(result, 'users')
@@ -125,12 +149,14 @@ describe('#UserRPC', () => {
it('should delete a user', async () => {
// Get the user model for the test user.
const testUserModel = await UserModel.findById(
testUserId,
testUser.userData._id,
'-password'
)
const result = await uut.deleteUser({}, testUserModel)
console.log(result)
await uut.deleteUser({}, testUserModel)
// console.log(result)
assert.isOk('Not throwing an error is a success')
})
})
})