Last active
March 15, 2019 13:53
-
-
Save jepetko/302a7dbca9dc0b7ab3dc9b42c8dea1bc to your computer and use it in GitHub Desktop.
FeathersJS Authentication test skeletton
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
describe('POST /users', () => { | |
describe('for correct user/password data', () => { | |
let notifierSpy: sinon.SinonSpy; | |
let mailerStub: sinon.SinonStub; | |
beforeEach(() => { | |
notifierSpy = sinon.spy(Notifier, 'onNotify'); | |
mailerStub = sinon.stub(app.service('mailer'), 'create'); | |
}); | |
afterEach(() => { | |
notifierSpy.restore(); | |
mailerStub.restore(); | |
}); | |
it('returns 201 for valid email and password', async () => { | |
const res = await rp(buildUsersPostRequest({ | |
email: notExistingValidUserEmail, | |
password: '123' | |
})); | |
expect(res.statusCode).to.eq(201); | |
const {body} = res; | |
expect(body.isVerified).to.eq(false); | |
usersBlacklistedProperties.forEach((prop) => expect(body).not.to.have.property(prop)); | |
}); | |
it('sends the verification email', async () => { | |
await rp(buildUsersPostRequest({ | |
email: notExistingValidUserEmail, | |
password: '123' | |
})); | |
const notifierCall = notifierSpy.lastCall; | |
const action = notifierCall.args[0] as string; | |
const passedUser = notifierCall.args[1] as User; | |
expect(action).to.eq('resendVerifySignup'); | |
const mailerCall = mailerStub.lastCall; | |
const args = mailerCall.args[0]; | |
expect(args).to.eql({ | |
from: 'mailer@server.io', | |
to: 'new-user@domain.com', | |
subject: 'Verify Signup', | |
html: `http://localhost:4200/signup/verify/${passedUser.verifyToken}` | |
}); | |
}); | |
}); | |
// ... | |
}); | |
describe('/authentication', () => { | |
let verifiedUser: User; | |
beforeEach(async () => { | |
verifiedUser = await fg.factory.create('verifiedUser'); | |
}); | |
describe('user credentials okay', () => { | |
it('returns a JWT', async () => { | |
const res = await rp(buildAuthenticationPostRequest({ | |
strategy: 'local', | |
email: verifiedUser.email, | |
password: DEFAULT_PASSWORD | |
})); | |
expect(res.statusCode).to.eq(201); | |
expect(res.body.accessToken).to.be.ok; | |
expect(res.body.accessToken.split('.').length).to.eq(3); | |
}); | |
}); | |
// ... | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment