Skip to content

Commit 7b696f8

Browse files
Merge pull request #17 from aayush-05/addMessageAPI
Add message api
2 parents 421d06d + f2c23cc commit 7b696f8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+9112
-1400
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules/
22
package-lock.json
33
.env
4+
.env.test

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# community-forum-backend
1+
# Spansberry Backend

__tests__/auth.spec.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
const app = require("../app").app;
2+
const supertest = require("supertest");
3+
const request = supertest(app);
4+
const mongoose = require("mongoose");
5+
const Organization = require("../models/organization");
6+
const User = require("../models/user");
7+
const server = require("../app").server;
8+
const jwt = require("jsonwebtoken");
9+
10+
let connection, userId;
11+
12+
beforeAll(async (done) => {
13+
connection = await server.listen(process.env.PORT);
14+
console.log(`Test: listening on ${process.env.PORT}`);
15+
await User.deleteMany({});
16+
await Organization.deleteMany({});
17+
const organizationResponse = await request
18+
.post("/graphql")
19+
.send({
20+
query: `mutation{ createOrganization(organizationInput: {
21+
name: "Test Organization"
22+
description: {
23+
shortDescription: "Lorem Ipsum"
24+
}
25+
contactInfo: {
26+
email: "test@email.com"
27+
website: "www.website.com"
28+
}
29+
}) {
30+
result
31+
}}`,
32+
})
33+
.set("Accept", "application/json");
34+
const userResponse = await request
35+
.post("/graphql")
36+
.send({
37+
query: `mutation{ createUser(userInput: {
38+
name: {
39+
firstName: "TestUser"
40+
lastName: "1"
41+
}
42+
email: "abc@email.com"
43+
password: "password"
44+
info: {
45+
about: {
46+
shortDescription: "Lorem Ipsum"
47+
}
48+
}
49+
}) {
50+
_id
51+
name {
52+
firstName
53+
lastName
54+
}
55+
email
56+
phone
57+
}}`,
58+
})
59+
.set("Accept", "application/json");
60+
userId = userResponse.body.data.createUser._id;
61+
await done();
62+
});
63+
64+
afterAll(async () => {
65+
await connection.close();
66+
await mongoose.connection.close();
67+
});
68+
69+
test("login existing user", async () => {
70+
const response = await request
71+
.post("/graphql")
72+
.send({
73+
query: `{ login(
74+
email: "abc@email.com"
75+
password: "password"
76+
) {
77+
name {
78+
firstName
79+
lastName
80+
}
81+
token
82+
} }`,
83+
})
84+
.set("Accept", "application/json");
85+
expect(response.type).toBe("application/json");
86+
expect(response.status).toBe(200);
87+
expect(response.body.data.login.name).toStrictEqual({
88+
firstName: "TestUser",
89+
lastName: "1",
90+
});
91+
const token = response.body.data.login.token;
92+
const decodedToken = jwt.verify(token, process.env.JWT_SECRET);
93+
expect(decodedToken.id).toBe(userId);
94+
});

0 commit comments

Comments
 (0)