Skip to content

Commit

Permalink
register user, logout user and retrieve user logic; retrieve user rest
Browse files Browse the repository at this point in the history
  • Loading branch information
berlem committed Mar 18, 2024
1 parent 43dba38 commit 8984e27
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 2 deletions.
2 changes: 1 addition & 1 deletion staff/belen-ivars/project/api/logic/retrieveUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ async function retrieveUser(id) {
const user = await User.findById(id)

if (!user) {
throw new NotFoundError("No user found :cry:")
throw new NotFoundError("No user found")
}

return user
Expand Down
51 changes: 51 additions & 0 deletions staff/belen-ivars/project/api/logic/retrieveUser.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import dotenv from 'dotenv'
dotenv.config()

import mongoose, { isObjectIdOrHexString, mongo } from 'mongoose'
import bcrypt from 'bcryptjs'

import { expect } from 'chai'
import random from './helpers/random.js'
import retrieveUser from './retrieveUser.js'
import { errors } from 'com'
import { User } from '../data/models.js'
import { NotFoundError } from 'com/errors.js'
const { DuplicityError } = errors
const { ObjectId } = mongoose.Types

describe('retrieveUser', () => {
before(async () => await mongoose.connect(process.env.TEST_MONGODB_URL))
beforeEach(async () => await User.deleteMany())

it('succeds on current user', async () => {

const name = random.name()
const email = random.email()
const password = random.password()

const user = await User.create({ name, email, password })
try {
await retrieveUser(user.id)

} catch (error) {

expect(user).to.exist
expect(user.name).to.equal(name)
expect(user.email).to.equal(email)
}


})

it('fails on non-existing user', async () => {
try {
await retrieveUser(new ObjectId().toString())
throw new Error('should not reach this point')
} catch (error) {
expect(error).to.be.instanceOf(NotFoundError)
expect(error.message).to.equal('No user found')
}
})

after(async () => await mongoose.disconnect())
})
15 changes: 15 additions & 0 deletions staff/belen-ivars/project/api/logic/retrieveUser.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import dotenv from 'dotenv'
dotenv.config()
import mongoose from 'mongoose'

import retrieveUser from './retrieveUser.js'

(async () => {
await mongoose.connect(process.env.MONGODB_URL)
try {
await retrieveUser('65d642e9695ce01b53585a85')
console.log('user retrieved')
} catch (error) {
console.log(error)
}
})()
7 changes: 7 additions & 0 deletions staff/belen-ivars/project/app/src/logic/logoutUser.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import session from "./session"

export default function logoutUser() {

session.token = null
session.sessionUserId = null


console.log('logging out')
}
2 changes: 1 addition & 1 deletion staff/belen-ivars/project/app/src/logic/registerUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default function registerUser(name, email, password) {
const req = {
method: 'POST',
headers: {
'Content-Type': 'applications/json'
'Content-Type': 'application/json'
},
body: JSON.stringify({ name, email, password })
}
Expand Down

0 comments on commit 8984e27

Please sign in to comment.