process.env.CAPTCHA_POW_DIFFICUTLY = "5000"; import { ResolvedCaptcha } from "@webresto/core/adapters/captcha/CaptchaAdapter"; import { Captcha } from "@webresto/core/adapters/index"; let Puzzle = require("fix-esm").require("crypto-puzzle").default; import OneTimePassword from "@webresto/core/models/OneTimePassword"; import { expect, assert } from "chai"; import { JWTAuth } from "../../lib/jwt"; // E2E import { runGQL } from "../e2e_helper"; let graphql; let serverInfo; describe("User", function () { let token; this.timeout(50000); // it("Login new user and setting password", async () => { // await sleep(3000); // graphql = // require("./../fixture/node_modules/@webresto/graphql/src/graphql").default; // let OTP = await getOTP(`1123`); // let captcha = await resolveCaptcha(`registration:1123`); // let phone = { code: "+1", number: "123" }; // const result = await graphql.getServer().executeOperation({ // query: `#graphql // mutation{login( // login: "1123", // phone: { code: "${phone.code}", number: "${phone.number}"}, // otp: "${OTP.password}", // password: "test123" // captcha: {id: "${captcha.id}", solution: ${captcha.solution}} // ){ // user { // id // phone { // number // code // } // lastName // } // message { // message // } // action { // type // data // } // }}`, // }); // if (result.errors) throw new Error(result.errors); // const response = result.data.registration; // expect(response.message.message).to.equal("New user created"); // assert.deepEqual(response.action, { // type: "GoTo", // data: { // section: "login", // delaySeconds: 5, // }, // }); // expect(response.user.id).to.not.be.null; // assert.deepEqual(response.user.phone, phone); // }); it("Login new user passwordPolicy = from_otp, LoginOTPrequired = false", async () => { graphql = require("./../fixture/node_modules/@webresto/graphql/src/graphql").default; let captcha = await resolveCaptcha(`login:1123`); let OTP = await getOTP(`1123`); let query = `#graphql mutation{login( login: "1123", otp: "${OTP.password}", phone: { code: "1", number: "123" }, captcha: {id: "${captcha.id}", solution: ${captcha.solution}} ){ message { message } action { type data } }}`; const loginMutation = await graphql.getServer().executeOperation({ query: query, }, {req: {rawHeaders: [ "X-Device-Id", "test001"]}} ); if (loginMutation.errors) throw new Error(loginMutation.errors); // token = loginMutation.data?.login?.action?.data?.token; // if (!token) throw `TOKEN JWT is not defined`; // const auth = await JWTAuth.verify(token); // let ud = await UserDevice.findOne({ sessionId: auth.sessionId }); // if (!ud) throw `Device not found by session`; // if (!ud.isLogined) throw `Session is not logined`; }); // it("Login user by password", async () => { // await Settings.set("LOGIN_OTP_REQUIRED", false); // token = await loginTestUserByPassword("Login user by password"); // if (!token) throw `TOKEN JWT is not defined`; // const auth = await JWTAuth.verify(token); // let ud = await UserDevice.findOne({ sessionId: auth.sessionId }); // if (!ud) throw `Device not found by session`; // if (!ud.isLogined) throw `Session is not logined`; // }); // ///////////////////////////////////////////////////////////////////// // describe("logout mutation", () => { // it("Logout user from current device", async () => { // let token = await loginTestUserByPassword("Test000"); // if (!token) throw `TOKEN JWT is not defined`; // const auth = await JWTAuth.verify(token); // let query = `#graphql // mutation { // logout { // message { // message // } // action { // type // data // } // } // }` // const logoutMutation: any = await runGQL(query, token); // if (logoutMutation.errors) throw new Error(logoutMutation.errors); // let ud = await UserDevice.findOne({ id: auth.deviceId }); // if (!ud) throw `Device not found by deviceName`; // if (ud.isLogined) throw `Session is still logined`; // }); // it("Logout user from specific device", async () => { // // 1st device // let token1 = await loginTestUserByPassword("Test001"); // if (!token1) throw `TOKEN2 JWT is not defined`; // // 2th device // let token2 = await loginTestUserByPassword("Test002"); // if (!token2) throw `TOKEN2 JWT is not defined`; // // logout from 2th device only // let query = `#graphql // mutation { // logout(deviceName: "Test002") { // message { // message // } // action { // type // data // } // } // }` // const logoutMutation: any = await runGQL(query, token); // if (logoutMutation.errors) throw new Error(logoutMutation.errors); // // 1th device should be logined // const auth0 = await JWTAuth.verify(token1); // let ud0 = await UserDevice.findOne({ // user: auth0.userId, // sessionId: auth0.sessionId, // }); // if (!ud0) throw `Device not found by deviceName`; // if (!ud0.isLogined) throw `Session 1 was logouted`; // // 2th device should not be logined // try { // await JWTAuth.verify(token2); // assert.fail('Should have thrown an error'); // } catch (error) { // assert.equal(error, 'Logined device not found'); // } // }); // }); // it('User query filter', async () => true); // // @webresto/graphql/lib/graphqlHelper.ts#390 // it('User subscription filter', async () => true); // @webresto/graphql/lib/graphqlHelper.ts#559 // it('Update user info', async () => true); // it('Update user custom fields', async () => true); // it('Update user password', async () => true); // it('Reset user password', async () => true); // it('Mutation RequestLogin & RequestRegistration just exist', async () => true); // it('Logout from current device', async () => true); // it('Logout from all device', async () => true); }); async function getOTP(login: string): Promise { let captcha = await resolveCaptcha(`OTPRequest:1123`); const _graphql = require("./../fixture/node_modules/@webresto/graphql/src/graphql").default; const gqlReq = await _graphql.getServer().executeOperation({ query: `#graphql mutation{OTPRequest(login: "${login}", captcha: {id: "${captcha.id}", solution: ${captcha.solution}}) { id nextOTPAfterSeconds } } `, }); if (gqlReq.errors) throw gqlReq.errors; let OTP = await OneTimePassword.findOne({ id: gqlReq.data.OTPRequest.id }); return OTP; } async function resolveCaptcha(label: string): Promise { const _graphql = require("./../fixture/node_modules/@webresto/graphql/src/graphql").default; const captcha = await _graphql.getServer().executeOperation({ query: `#graphql { captchaGetJob(label: "${label}") { id task } } `, }); if (captcha.errors) throw captcha.errors; let id = captcha.data.captchaGetJob.id; let task = JSON.parse(captcha.data.captchaGetJob.task); let parsedTask = { difficulty: parseInt(task.difficulty), salt: task.salt, hash: task.hash, }; let resolvedTask = await Puzzle.solve(parsedTask); let resolvedCaptcha = { id: id, solution: JSON.stringify(resolvedTask, (key, value) => typeof value === "bigint" ? value.toString() : value ), }; return resolvedCaptcha; } async function loginTestUserByPassword( deviceName: string ): Promise { await Settings.set("PASSWORD_REQUIRED", true); await Settings.set("LOGIN_OTP_REQUIRED", false); const _graphql = require("./../fixture/node_modules/@webresto/graphql/src/graphql").default; let captcha = await resolveCaptcha(`login:1123`); let query = `#graphql mutation { login( login: "1123", password: "test123", deviceName: "${deviceName}", captcha: {id: "${captcha.id}", solution: ${captcha.solution}} ) { message { message } action { type data } } }`; const loginMutation = await _graphql.getServer().executeOperation({ query: query, }); if (loginMutation.errors) throw new Error(loginMutation.errors); return loginMutation.data?.login?.action?.data?.token; } function e2eMutation(query, token) {} function sleep(ms) { return new Promise((resolve) => setTimeout(resolve, ms)); }