Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 1s
141 lines
3.9 KiB
TypeScript
141 lines
3.9 KiB
TypeScript
import { randomBytes } from 'crypto';
|
|
|
|
import type { ILoginCode, IUser } from '@rocket.chat/core-typings';
|
|
import { expect } from 'chai';
|
|
import { after, before, describe, it } from 'mocha';
|
|
import { MongoClient } from 'mongodb';
|
|
|
|
import { api, request, getCredentials } from '../../data/api-data';
|
|
import type { TestUser } from '../../data/users.helper';
|
|
import { createUser, deleteUser } from '../../data/users.helper';
|
|
import { URL_MONGODB } from '../../e2e/config/constants';
|
|
|
|
const CODE_TTL_MS = 60 * 1000;
|
|
|
|
async function insertLoginCode(connection: MongoClient, userId: string, insertExpiredToken: boolean = false): Promise<string> {
|
|
const now = new Date();
|
|
const code = randomBytes(32).toString('hex');
|
|
|
|
let expireAt = new Date(now.getTime() + CODE_TTL_MS);
|
|
|
|
if (insertExpiredToken) {
|
|
expireAt = new Date(now.getTime() - 1);
|
|
}
|
|
|
|
await connection.db().collection<ILoginCode>('rocketchat_login_codes').insertOne({
|
|
_id: code,
|
|
userId,
|
|
createdAt: now,
|
|
expireAt,
|
|
});
|
|
|
|
return code;
|
|
}
|
|
|
|
describe('[Login Codes]', () => {
|
|
let connection: MongoClient;
|
|
|
|
before((done) => getCredentials(done));
|
|
|
|
before(async () => {
|
|
connection = await MongoClient.connect(URL_MONGODB);
|
|
});
|
|
|
|
after(async () => {
|
|
await connection.close();
|
|
});
|
|
|
|
describe('POST [/loginCode.redeem]', () => {
|
|
let testUser: TestUser<IUser>;
|
|
|
|
before(async () => {
|
|
testUser = await createUser();
|
|
});
|
|
|
|
after(async () => {
|
|
await deleteUser(testUser);
|
|
});
|
|
|
|
it('should fail when code does not exist in the database', async () => {
|
|
await request
|
|
.post(api('loginCode.redeem'))
|
|
.send({ code: 'a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2' })
|
|
.expect(400)
|
|
.expect((res) => {
|
|
expect(res.body).to.have.property('success', false);
|
|
});
|
|
});
|
|
|
|
it('should fail when code is an empty string', async () => {
|
|
await request
|
|
.post(api('loginCode.redeem'))
|
|
.send({ code: '' })
|
|
.expect(400)
|
|
.expect((res) => {
|
|
expect(res.body).to.have.property('success', false);
|
|
expect(res.body).to.have.property('errorType', 'invalid-params');
|
|
});
|
|
});
|
|
|
|
it('should return a loginToken and userId for a valid code', async () => {
|
|
const code = await insertLoginCode(connection, testUser._id);
|
|
|
|
await request
|
|
.post(api('loginCode.redeem'))
|
|
.send({ code })
|
|
.expect(200)
|
|
.expect((res) => {
|
|
expect(res.body).to.have.property('success', true);
|
|
expect(res.body).to.have.property('loginToken').that.is.a('string').and.is.not.empty;
|
|
expect(res.body).to.have.property('userId', testUser._id);
|
|
});
|
|
});
|
|
|
|
it('should invalidate a code after it has been redeemed', async () => {
|
|
const code = await insertLoginCode(connection, testUser._id);
|
|
|
|
await request.post(api('loginCode.redeem')).send({ code }).expect(200);
|
|
|
|
await request
|
|
.post(api('loginCode.redeem'))
|
|
.send({ code })
|
|
.expect(400)
|
|
.expect((res) => {
|
|
expect(res.body).to.have.property('success', false);
|
|
expect(res.body).to.have.property('error', 'error-invalid-code');
|
|
});
|
|
});
|
|
|
|
it('should return a loginToken that can be used for subsequent authenticated requests', async () => {
|
|
const code = await insertLoginCode(connection, testUser._id);
|
|
|
|
const redeemRes = await request.post(api('loginCode.redeem')).send({ code }).expect(200);
|
|
|
|
const { loginToken, userId } = redeemRes.body;
|
|
|
|
await request
|
|
.get(api('me'))
|
|
.set('X-Auth-Token', loginToken)
|
|
.set('X-User-Id', userId)
|
|
.expect(200)
|
|
.expect((res) => {
|
|
expect(res.body).to.have.property('success', true);
|
|
expect(res.body).to.have.property('_id', testUser._id);
|
|
});
|
|
});
|
|
|
|
it('should fail when code is expired', async () => {
|
|
const code = await insertLoginCode(connection, testUser._id, true);
|
|
|
|
await request
|
|
.post(api('loginCode.redeem'))
|
|
.send({ code })
|
|
.expect(400)
|
|
.expect((res) => {
|
|
expect(res.body).to.have.property('success', false);
|
|
expect(res.body).to.have.property('error', 'error-invalid-code');
|
|
});
|
|
});
|
|
});
|
|
});
|