diff --git a/__tests__/RealTimeAPI.test.ts b/__tests__/RealTimeAPI.test.ts index 6b6a57d..b5c4e19 100644 --- a/__tests__/RealTimeAPI.test.ts +++ b/__tests__/RealTimeAPI.test.ts @@ -1,5 +1,6 @@ import { WebSocket, Server } from "mock-socket"; import { RealTimeAPI } from "../src/index"; +import { SHA256 } from "crypto-js"; describe("RealTimeAPI tests", () => { const url = "ws://localhost:8080/"; @@ -44,4 +45,173 @@ describe("RealTimeAPI tests", () => { }); }); }); + + it("can send connection request message", done => { + const realtimeAPI$ = new RealTimeAPI(url); + + realtimeAPI$.connectToServer().subscribe(); // Should send pong to every ping message. + + mockServer.on("connection", (socket: WebSocket) => { + expect(socket.url).toEqual(url); // Expecting websocket url. + + socket.on("message", data => { + expect(data).toEqual( + JSON.stringify({ + msg: "connect", + version: "1", + support: ["1", "pre2", "pre1"] + }) + ); // Expecting ddp connection message. + done(); + }); + }); + }); + + it("can send login request with username and password", done => { + const realtimeAPI$ = new RealTimeAPI(url); + const username = "username"; + const password = "password"; + realtimeAPI$.login(username, password).subscribe(); // Should send pong to every ping message. + + mockServer.on("connection", (socket: WebSocket) => { + expect(socket.url).toEqual(url); // Expecting websocket url. + + socket.on("message", data => { + let message = JSON.parse(data); + + expect(message).toHaveProperty("id"); // Expecting to have "id" property in message. + + expect(message).toHaveProperty("msg"); // Expecting to have "msg" property in message. + expect(message.msg).toEqual("method"); // Expecting "msg" to be "method" in message. + + expect(message).toHaveProperty("method"); // Expecting to have "method" property in message. + expect(message.method).toEqual("login"); // Expecting "method" to be "login" in message. + + expect(message).toHaveProperty("params"); // Expecting to have "params" property in message. + + expect(message.params).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + user: { username }, + password: { + digest: SHA256(password).toString(), + algorithm: "sha-256" + } + }) + ]) + ); //Expecting params to be Array of Object { user: {username: "username"}, password: { algorithm: "sha-256", digest: "..."} } + + done(); + }); + }); + }); + + it("can send login request with email and password", done => { + const realtimeAPI$ = new RealTimeAPI(url); + const email = "username@email.com"; + const password = "password"; + realtimeAPI$.login(email, password).subscribe(); // Should send pong to every ping message. + mockServer.on("connection", (socket: WebSocket) => { + expect(socket.url).toEqual(url); // Expecting websocket url. + + socket.on("message", data => { + let message = JSON.parse(data); + + expect(message).toHaveProperty("id"); // Expecting to have "id" property in message. + + expect(message).toHaveProperty("msg"); // Expecting to have "msg" property in message. + expect(message.msg).toEqual("method"); // Expecting "msg" to be "method" in message. + + expect(message).toHaveProperty("method"); // Expecting to have "method" property in message. + expect(message.method).toEqual("login"); // Expecting "method" to be "login" in message. + + expect(message).toHaveProperty("params"); // Expecting to have "params" property in message. + + expect(message.params).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + user: { email }, + password: { + digest: SHA256(password).toString(), + algorithm: "sha-256" + } + }) + ]) + ); //Expecting params to be Array of Object { user: {email: "username@email.com"}, password: { algorithm: "sha-256", digest: "..."} } + + done(); + }); + }); + }); + + it("can send login request with auth token", done => { + const realtimeAPI$ = new RealTimeAPI(url); + const token = "token"; + realtimeAPI$.loginWithAuthToken(token).subscribe(); // Should send pong to every ping message. + mockServer.on("connection", (socket: WebSocket) => { + expect(socket.url).toEqual(url); // Expecting websocket url. + + socket.on("message", data => { + let message = JSON.parse(data); + + expect(message).toHaveProperty("id"); // Expecting to have "id" property in message. + + expect(message).toHaveProperty("msg"); // Expecting to have "msg" property in message. + expect(message.msg).toEqual("method"); // Expecting "msg" to be "method" in message. + + expect(message).toHaveProperty("method"); // Expecting to have "method" property in message. + expect(message.method).toEqual("login"); // Expecting "method" to be "login" in message. + + expect(message).toHaveProperty("params"); // Expecting to have "params" property in message. + + expect(message.params).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + resume: token + }) + ]) + ); //Expecting params to be Array of Object { resume: "token" } + + done(); + }); + }); + }); + + it("can send login request with oauth tokens", done => { + const realtimeAPI$ = new RealTimeAPI(url); + const credentialToken = "credentialToken"; + const credentialSecret = "credentialSecret"; + + realtimeAPI$.loginWithOAuth(credentialToken, credentialSecret).subscribe(); // Should send pong to every ping message. + mockServer.on("connection", (socket: WebSocket) => { + expect(socket.url).toEqual(url); // Expecting websocket url. + + socket.on("message", data => { + let message = JSON.parse(data); + + expect(message).toHaveProperty("id"); // Expecting to have "id" property in message. + + expect(message).toHaveProperty("msg"); // Expecting to have "msg" property in message. + expect(message.msg).toEqual("method"); // Expecting "msg" to be "method" in message. + + expect(message).toHaveProperty("method"); // Expecting to have "method" property in message. + expect(message.method).toEqual("login"); // Expecting "method" to be "login" in message. + + expect(message).toHaveProperty("params"); // Expecting to have "params" property in message. + + expect(message.params).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + oauth: { + credentialToken, + credentialSecret + } + }) + ]) + ); //Expecting params to be Array of Object { oauth: {credentialSecret: "credentialSecret", credentialToken: "credentialToken"} } + + done(); + }); + }); + }); });