0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-01-06 22:40:26 -05:00
verdaccio/packages/api/test/integration/whoami.spec.ts

36 lines
1.3 KiB
TypeScript
Raw Normal View History

import supertest from 'supertest';
import { HEADERS, HTTP_STATUS, TOKEN_BEARER } from '@verdaccio/core';
import { buildToken } from '@verdaccio/utils';
import { createUser, initializeServer } from './_helper';
describe('whoami', () => {
test('should return the logged username', async () => {
const app = await initializeServer('whoami.yaml');
// @ts-expect-error internal property
const { _body } = await createUser(app, 'test', 'test');
return supertest(app)
.get('/-/whoami')
.set('Accept', HEADERS.JSON)
.set(HEADERS.AUTHORIZATION, buildToken(TOKEN_BEARER, _body.token))
.expect('Content-Type', HEADERS.JSON_CHARSET)
.expect(HTTP_STATUS.OK)
.then((response) => {
expect(response.body.username).toEqual('test');
});
});
test('should fails with 401 if is not logged in', async () => {
const app = await initializeServer('whoami.yaml');
// @ts-expect-error internal property
const { _body } = await createUser(app, 'test', 'test');
return supertest(app)
.get('/-/whoami')
.set('Accept', HEADERS.JSON)
.set(HEADERS.AUTHORIZATION, buildToken('invalid-token', _body.token))
.expect('Content-Type', HEADERS.JSON_CHARSET)
.expect(HTTP_STATUS.UNAUTHORIZED);
});
});