-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[test] | ||
coverage = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const generateRandomEmail = () => { | ||
const randomString = Math.random().toString(36).substring(2, 12); // Random alphanumeric string | ||
const domains = ["example.com", "test.com", "email.com", "mock.com"]; | ||
const randomDomain = domains[Math.floor(Math.random() * domains.length)]; | ||
return `${randomString}@${randomDomain}`; | ||
}; | ||
|
||
export { generateRandomEmail }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { describe, expect, it } from "bun:test"; | ||
import { api } from "../src"; | ||
import { generateRandomEmail } from "@utils/mockUtils"; | ||
|
||
describe("/v2/login Auth Sign In", () => { | ||
it("If Email / Password cant be null", async () => { | ||
|
@@ -61,3 +62,85 @@ describe("/v2/login Auth Sign In", () => { | |
expect(response.body.message).toBe("Password is invalid."); | ||
}); | ||
}); | ||
|
||
describe("/v2/signup Auth Sign Up", () => { | ||
it("should return 201 if user can register with valid data", async () => { | ||
const response = await api | ||
.handle( | ||
new Request("http://localhost:3121/v2/auth/signup", { | ||
method: "POST", | ||
headers: { "Content-Type": "application/json" }, | ||
body: JSON.stringify({ | ||
email: generateRandomEmail(), | ||
password: "password123", | ||
name: "Random User", | ||
}), | ||
}) | ||
) | ||
.then(async (res) => ({ | ||
status: res.status, | ||
body: await res.json(), | ||
})); | ||
|
||
expect(response.status).toBe(201); | ||
}); | ||
|
||
it("should return 422 if email or password is missing", async () => { | ||
const response = await api | ||
.handle( | ||
new Request("http://localhost:3121/v2/auth/signup", { | ||
method: "POST", | ||
headers: { "Content-Type": "application/json" }, | ||
body: JSON.stringify({ | ||
name: "Test User", | ||
}), | ||
}) | ||
) | ||
.then(async (res) => ({ | ||
status: res.status, | ||
body: await res.json(), | ||
})); | ||
|
||
expect(response.status).toBe(422); | ||
}); | ||
|
||
it("should return 422 if password is missing", async () => { | ||
const response = await api | ||
.handle( | ||
new Request("http://localhost:3121/v2/auth/signup", { | ||
method: "POST", | ||
headers: { "Content-Type": "application/json" }, | ||
body: JSON.stringify({ | ||
email: "[email protected]", | ||
name: "Test User", | ||
}), | ||
}) | ||
) | ||
.then(async (res) => ({ | ||
status: res.status, | ||
body: await res.json(), | ||
})); | ||
|
||
expect(response.status).toBe(422); | ||
}); | ||
|
||
it("should return 422 if email is missing", async () => { | ||
const response = await api | ||
.handle( | ||
new Request("http://localhost:3121/v2/auth/signup", { | ||
method: "POST", | ||
headers: { "Content-Type": "application/json" }, | ||
body: JSON.stringify({ | ||
password: "password123", | ||
name: "Test User", | ||
}), | ||
}) | ||
) | ||
.then(async (res) => ({ | ||
status: res.status, | ||
body: await res.json(), | ||
})); | ||
|
||
expect(response.status).toBe(422); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { describe, expect, it } from "bun:test"; | ||
import { api } from "../src"; | ||
|
||
describe("Chat Modules", () => { | ||
it("Show chat item, chat information & user information", async () => { | ||
const response = await api | ||
.handle(new Request("http://localhost:3121/v2/message")) | ||
.then(async (res) => await res.json()); | ||
|
||
expect(response).toHaveProperty("status", 200); | ||
expect(response).toHaveProperty("data"); | ||
expect(Array.isArray(response.data)).toBe(true); | ||
|
||
if (response.data.length > 0) { | ||
const message = response.data[0]; | ||
|
||
expect(message).toHaveProperty("id"); | ||
expect(message).toHaveProperty("user"); | ||
expect(message).toHaveProperty("mentionedBy"); | ||
expect(message).toHaveProperty("mentionedTo"); | ||
expect(message).toHaveProperty("createdAt"); | ||
|
||
expect(message.user).toHaveProperty("id"); | ||
expect(message.user).toHaveProperty("name"); | ||
expect(message.user).toHaveProperty("email"); | ||
expect(message.user).toHaveProperty("iconUrl"); | ||
expect(message.user).toHaveProperty("isAdmin"); | ||
|
||
if (message?.mentionedTo?.length > 0) { | ||
const mentioned = message.mentionedTo[0]; | ||
expect(mentioned).toHaveProperty("id"); | ||
expect(mentioned).toHaveProperty("message"); | ||
expect(mentioned).toHaveProperty("user"); | ||
expect(mentioned.user).toHaveProperty("name"); | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
// it("Should create a message and receive it via WebSocket", async (done) => { | ||
// const ws = treaty<typeof api>("localhost:3000"); | ||
// const chat = api.subscribe() | ||
|
||
// ws.onopen = () => { | ||
// ws.send( | ||
// JSON.stringify({ | ||
// action: "CREATE", | ||
// message: "Hello, world!", | ||
// }) | ||
// ); | ||
// }; | ||
|
||
// ws.onmessage = (event) => { | ||
// const data = JSON.parse(event.data); | ||
|
||
// expect(data).toHaveProperty("id"); | ||
// expect(data).toHaveProperty("message", "Hello, world!"); | ||
// expect(data).toHaveProperty("user"); | ||
|
||
// ws.close(); | ||
// done(); | ||
// }; | ||
|
||
// ws.onerror = (err) => { | ||
// ws.close(); | ||
// done(err); | ||
// }; | ||
// }); | ||
// }); |