forked from PinSpace-Org/GistPin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgists.e2e.spec.ts
More file actions
180 lines (150 loc) 路 5.69 KB
/
Copy pathgists.e2e.spec.ts
File metadata and controls
180 lines (150 loc) 路 5.69 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication, ValidationPipe } from '@nestjs/common';
import * as request from 'supertest';
import { AppModule } from 'src/app.module';
import { AllExceptionsFilter } from 'src/common/filters/all-exceptions.filter';
describe('Gists (e2e)', () => {
let app: INestApplication;
beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = moduleFixture.createNestApplication();
app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
transform: true,
forbidNonWhitelisted: true,
}),
);
app.useGlobalFilters(new AllExceptionsFilter());
await app.init();
});
afterAll(async () => {
await app.close();
});
describe('POST /gists', () => {
it('should create a gist and return 201 with full shape', async () => {
const res = await request(app.getHttpServer())
.post('/gists')
.send({ content: 'e2e test gist', lat: 9.0579, lon: 7.4951 })
.expect(201);
expect(res.body).toMatchObject({
id: expect.any(String),
content: 'e2e test gist',
location_cell: expect.any(String),
content_hash: expect.stringContaining('mock_'),
stellar_gist_id: expect.any(String),
tx_hash: expect.stringContaining('mock_'),
created_at: expect.any(String),
lat: 9.0579,
lon: 7.4951,
});
});
it('should return 400 when lat is out of range', async () => {
const res = await request(app.getHttpServer())
.post('/gists')
.send({ content: 'bad lat', lat: 999, lon: 7.4951 })
.expect(400);
expect(res.body.statusCode).toBe(400);
expect(res.body.message).toEqual(expect.arrayContaining([expect.stringContaining('lat')]));
});
it('should return 400 when content exceeds 280 characters', async () => {
const res = await request(app.getHttpServer())
.post('/gists')
.send({ content: 'x'.repeat(281), lat: 9.0579, lon: 7.4951 })
.expect(400);
expect(res.body.statusCode).toBe(400);
});
it('should return 400 when required fields are missing', async () => {
await request(app.getHttpServer())
.post('/gists')
.send({ content: 'missing coords' })
.expect(400);
});
it('should reject unknown fields (whitelist)', async () => {
await request(app.getHttpServer())
.post('/gists')
.send({ content: 'whitelist test', lat: 9.0579, lon: 7.4951, hack: 'injected' })
.expect(400);
});
});
describe('GET /gists', () => {
it('should return paginated response with data and pagination', async () => {
const res = await request(app.getHttpServer())
.get('/gists')
.query({ lat: 9.0579, lon: 7.4951, radius: 1000 })
.expect(200);
expect(res.body).toMatchObject({
data: expect.any(Array),
pagination: {
count: expect.any(Number),
hasMore: expect.any(Boolean),
},
});
});
it('should return 400 when lat/lon are missing', async () => {
await request(app.getHttpServer()).get('/gists').expect(400);
});
it('should respect the limit parameter', async () => {
const res = await request(app.getHttpServer())
.get('/gists')
.query({ lat: 9.0579, lon: 7.4951, limit: 2 })
.expect(200);
expect(res.body.data.length).toBeLessThanOrEqual(2);
});
}); describe('GET /gists/:id', () => {
it('should return the gist when it exists', async () => {
const created = await request(app.getHttpServer())
.post('/gists')
.send({ content: 'find me by id', lat: 9.0579, lon: 7.4951 })
.expect(201);
const res = await request(app.getHttpServer())
.get(`/gists/${created.body.id}`)
.expect(200);
expect(res.body).toMatchObject({
id: created.body.id,
content: 'find me by id',
location_cell: expect.any(String),
content_hash: expect.any(String),
stellar_gist_id: expect.any(String),
tx_hash: expect.any(String),
lat: 9.0579,
lon: 7.4951,
});
expect(res.body.created_at).toEqual(expect.any(String));
});
it('should return 404 when no gist matches the UUID', async () => {
const res = await request(app.getHttpServer())
.get('/gists/00000000-0000-0000-0000-000000000000')
.expect(404);
expect(res.body.statusCode).toBe(404);
expect(res.body.message).toEqual(expect.stringContaining('not found'));
});
it('should return 400 when id is not a valid UUID', async () => {
const res = await request(app.getHttpServer())
.get('/gists/not-a-uuid')
.expect(400);
expect(res.body.statusCode).toBe(400);
});
it('should return 400 when id contains dangerous characters', async () => {
// Raw SQL would reject this anyway, but ParseUUIDPipe catches it first.
const res = await request(app.getHttpServer())
.get("/gists/'; DROP TABLE gists; --")
.expect(400);
expect(res.body.statusCode).toBe(400);
// Confirm the failure came from UUID validation rather than URL parsing
expect(res.body.message).toEqual(
expect.arrayContaining([expect.stringMatching(/UUID|validation/i)]),
);
});
});
describe('GET /health', () => {
it('should return ok status', async () => {
const res = await request(app.getHttpServer()).get('/health').expect(200);
expect(res.body.status).toBe('ok');
expect(res.body.services.database.status).toBe('ok');
expect(res.body.services.postgis.status).toBe('ok');
});
});
});