-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.test.js
106 lines (95 loc) · 3.82 KB
/
app.test.js
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
const request = require('supertest');
const { app, db } = require('./server'); // Import db along with the app
describe('API Endpoints', () => {
// Test User Registration
it('should create a new user', async () => {
const uniqueEmail = `test_${Date.now()}@example.com`; // Generates a unique email
const res = await request(app)
.post('/api/users')
.send({
username: 'testuser',
email: uniqueEmail,
password: 'password123'
});
expect(res.statusCode).toEqual(201);
expect(res.body).toHaveProperty('id');
});
// Test Data Submission
it('should submit environmental data', async () => {
const timestamp = new Date().toISOString().replace('T', ' ').replace(/\.\d+Z$/, '');
const res = await request(app)
.post('/api/data')
.send({
user_id: 1,
data_type: 'Air Quality',
value: 42,
timestamp: timestamp,
location: 'Test Location'
});
expect(res.statusCode).toEqual(201);
});
// Test Retrieving Data
it('should retrieve all environmental data', async () => {
const res = await request(app)
.get('/api/data');
expect(res.statusCode).toEqual(200);
expect(res.body).toBeInstanceOf(Array);
});
// Test User Retrieval
it('should retrieve a user', async () => {
const res = await request(app)
.get('/api/users/1'); // Assuming user_id 1 exists
expect(res.statusCode).toEqual(200);
expect(res.body).toHaveProperty('username');
});
// Test Alert Creation
it('should create an alert', async () => {
const timestamp = new Date().toISOString().replace('T', ' ').replace(/\.\d+Z$/, '');
const res = await request(app)
.post('/api/alerts')
.send({
user_id: 1, // Ensure this user_id exists in your test database
message: 'Test Alert',
threshold: 5,
timestamp: timestamp
});
expect(res.statusCode).toEqual(201);
});
// Test Alert Retrieval
it('should retrieve all alerts', async () => {
const res = await request(app).get('/api/alerts');
expect(res.statusCode).toEqual(200);
expect(res.body).toBeInstanceOf(Array);
});
// Test Community Report Creation
it('should create a community report', async () => {
const timestamp = new Date().toISOString().replace('T', ' ').replace(/\.\d+Z$/, '');
const res = await request(app)
.post('/api/reports')
.send({
user_id: 1, // Ensure this user_id exists in your test database
issue_type: 'Pollution',
description: 'Test Description',
timestamp: timestamp,
location: 'Test Location'
});
expect(res.statusCode).toEqual(201);
});
// Test Community Report Retrieval
it('should retrieve all community reports', async () => {
const res = await request(app).get('/api/reports');
expect(res.statusCode).toEqual(200);
expect(res.body).toBeInstanceOf(Array);
});
// Test Retrieving User Sustainability Score
it('should retrieve a user sustainability score', async () => {
const res = await request(app)
.get('/api/users/1/score'); // Replace with a valid user_id
expect(res.statusCode).toEqual(200);
expect(res.body).toHaveProperty('score');
});
// Add tests for /api/resources and /api/research-data if needed
});
afterAll(() => {
db.end(); // Close the database connection after tests
});