-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathwhoami.ts
105 lines (94 loc) · 2.43 KB
/
whoami.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { FastifyInstance } from 'fastify';
import { User } from '../src/entity';
import request from 'supertest';
import {
authorizeRequest,
disposeGraphQLTesting,
GraphQLTestClient,
GraphQLTestingState,
initializeGraphQLTesting,
MockContext,
} from './helpers';
import { userCreatedDate, usersFixture } from './fixture/user';
import { DataSource } from 'typeorm';
import createOrGetConnection from '../src/db';
import { DEFAULT_TIMEZONE } from '../src/common';
let app: FastifyInstance;
let con: DataSource;
let state: GraphQLTestingState;
let client: GraphQLTestClient;
let loggedUser: string = null;
beforeAll(async () => {
con = await createOrGetConnection();
state = await initializeGraphQLTesting(
() => new MockContext(con, loggedUser),
);
client = state.client;
app = state.app;
});
afterAll(() => disposeGraphQLTesting(state));
beforeEach(async () => {
loggedUser = null;
await con.getRepository(User).save({ ...usersFixture[0] });
});
describe('query whoami', () => {
const QUERY = `{
whoami {
id
name
image
createdAt
username
bio
twitter
github
hashnode
infoConfirmed
timezone
}
}`;
it('should return null if anonymous', async () => {
const res = await client.query(QUERY);
expect(res.data.whoami).toEqual(null);
});
it('should return whoami', async () => {
loggedUser = '1';
const res = await client.query(QUERY);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { email, ...user } = usersFixture[0];
expect(res.data.whoami).toEqual({
...user,
timezone: DEFAULT_TIMEZONE,
createdAt: userCreatedDate,
});
});
});
describe('dedicated api routes', () => {
describe('GET /whoami', () => {
it('should return whoami data', async () => {
loggedUser = '1';
const res = await authorizeRequest(
request(app.server).get('/whoami'),
).expect(200);
expect(res.body).toEqual({
...usersFixture[0],
company: null,
portfolio: null,
title: null,
timezone: DEFAULT_TIMEZONE,
createdAt: userCreatedDate,
reputation: 10,
acceptedMarketing: false,
notificationEmail: true,
roadmap: null,
threads: null,
codepen: null,
reddit: null,
stackoverflow: null,
youtube: null,
linkedin: null,
mastodon: null,
});
});
});
});