diff --git a/examples/README.md b/examples/README.md index 8c8cca9..11aa0f0 100644 --- a/examples/README.md +++ b/examples/README.md @@ -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": ""}}` +- `{"jsonrpc":"2.0","id":"123","method":"users","params":{ "endpoint": "deleteUser", "userId": "<_id>", "apiToken": ""}}` diff --git a/src/rpc/users/index.js b/src/rpc/users/index.js index 12f3839..82db84c 100644 --- a/src/rpc/users/index.js +++ b/src/rpc/users/index.js @@ -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) diff --git a/test/unit/json-rpc/a13-users.unit.js b/test/unit/json-rpc/a13-users.unit.js index af53ffe..fca2a32 100644 --- a/test/unit/json-rpc/a13-users.unit.js +++ b/test/unit/json-rpc/a13-users.unit.js @@ -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') }) }) })