forked from b00tc4mp/isdi-parttime-202309
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
register user, logout user and retrieve user logic; retrieve user rest …
- Loading branch information
Showing
5 changed files
with
75 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters