SINON - an issue mocking a middleware - javascript

I have the following middleware in a Node.js REST API
const Authorized = (req, res, next) => {
if (!req.headers["authorization"]) {
res.status(401).send("Unauthorized");
} else {
jwt.verify(req.headers["authorization"], PRIVATE_KEY, (err, decoded) => {
if (err) {
res.status(403).send("Forbidden");
} else {
req.businessId = decoded.businessId;
req.roleId = decoded.roleId;
next();
}
});
}
};
As you can see I'm adding to variables to the request object
In the mockup of my tests I'm trying to do this:
sandbox = sinon.createSandbox();
sandbox.stub(Authorized, "Authorized")
.callsFake(async (req, res, next) => {
req.businessId = businessAux.id;
return next();
});
But this doesn't work and my actual function to be tested needs this variable:
listPermissionAndRolesByPk() {
this.app.get(`${config.API_PATH}/permissionrolebypk`, Authorized.Authorized, async (req, res) => {
const id = req.query.id;
const businessId = req.businessId;
if (!id) return res.status(400).send(messages[23].message.replace("${object}", "Permission Id"));
try {
const permission = await PermissionDAO.getPermissionAndRolesByPk(id, businessId ? businessId : 0);
if (permission) {
return res.status(200).json(permission);
} else {
return res.status(404).send(messages[8].message.replace("${object}", "Permission"));
}
} catch (error) {
return res.status(error.httpCode).send(error.message);
}
});
}
Any help will be appreciated.

Related

Redis client.get not working but client.set is working

async function getRepos(req, res, next) {
try {
console.log('Fetching Data...');
const { username } = req.params;
const response = await fetch(`https://api.github.com/users/${username}`);
const data = await response.json();
const repos = data.public_repos;
// Set data to Redis
await client.setex(username, 3600, repos);
res.send(setResponse(username, repos));
} catch (err) {
console.error(err);
res.status(500);
}
}
// Cache middleware
async function cache(req, res, next) {
const { username } = req.params;
console.log(username)
await client.get(username, (err, data) => {
if (err) throw err;
if (data !== null) {
res.send(setResponse(username, data));
} else {
next();
}
});
}
When this code executes the set function works(checked by using redis-cli).But the get function does not work when I send the request to the api.
You're mixing the Redis client's promise API with the callback API. Don't set a callback if you want to use await and vice versa.
// Cache middleware
async function cache(req, res, next) {
const {username} = req.params;
console.log(username);
const data = await client.get(username);
if (data !== null) {
res.send(setResponse(username, data));
} else {
next();
}
}

Using SocketIO with Unity Quest 2 vr as client and Node.JS as server

Me and my team are building a VR game for the Quest 2 with Unity written in C#.
We have a program that is the server that is written in JavaScript (Node.JS), the way that they are both communicating is through SocketIO. With in the Unity editor both applications are able to talk to each other, but when I try to build the app as an APK and load it onto the Quest 2, it appears that the server does not talk to the VR game.
Any advice on what I could do to have them talk to each other? Here is the NetworkManager file that I have been using.
Note: Anything with SocketIO in the file always has a red line under and says "The type or namespace name 'SocketIOCommunicator' could not be found (are you missing a using directive or an assembly reference?) [Assembly-CSharp]csharp(CS0246)"
I dont know what this means and what to do.
using System;
using System.Collections;
using Firesplash.UnityAssets.SocketIO;
using Newtonsoft.Json;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using static PlayerManager;
using Classes.Managers;
namespace Network
{
public class JoinRoom
{
public string clinician;
}
public class StartGame
{
public string game;
}
public static class Pausing
{
public static Boolean isPaused = false;
}
public class Position
{
public float xPos;
public float zPos;
public float yPos;
}
[RequireComponent(typeof(SocketIOCommunicator))]
public class NetworkManager : MonoBehaviour
{
private SocketIOCommunicator _communicator;
private SocketIOInstance _socket;
[SerializeField] private TMP_InputField patientNameInput;
[SerializeField] private TextMeshProUGUI patientName;
[SerializeField] private TextMeshProUGUI clinicianName;
[SerializeField] private GameObject setName;
[SerializeField] private TextMeshProUGUI connText;
private void Awake()
{
DontDestroyOnLoad(this);
_communicator = GetComponent<SocketIOCommunicator>();
_socket = _communicator.Instance;
}
private void Start()
{
_socket.Connect();
HandleServerEvents();
StartCoroutine(PatientConnect());
}
private IEnumerator PatientConnect()
{
yield return new WaitUntil(() => _socket.IsConnected());
FetchName();
}
private void FetchName()
{
if (PlayerPrefs.HasKey("PatientName"))
{
PlayerPrefs.SetString("PatientName", "Test Subject FetchName()");
var name = PlayerPrefs.GetString("PatientName");
Debug.Log("Has Name: " + name);
_socket.Emit("unityConnect", name);
patientName.SetText(name);
SwitchToViewName();
}
else
{
SwitchToSetName();
}
}
private void HandleServerEvents()
{
_socket.On("userJoined", (string payload) =>
{
Debug.Log("Joined Room!");
var obj = JsonConvert.DeserializeObject<JoinRoom>(payload);
Debug.Log(obj.clinician);
clinicianName.SetText(obj.clinician);
connText.gameObject.SetActive(true);
});
_socket.On("startGame", (string payload) =>
{
Debug.Log("Started Game");
var obj = JsonConvert.DeserializeObject<StartGame>(payload);
Debug.Log(obj.game);
// connText.SetText("Started Planes");
switch(obj.game) {
case "3":
SceneManager.LoadScene("Planes");
break;
case "2":
SceneManager.LoadScene("Balloons");
break;
case "1":
SceneManager.LoadScene("Blocks");
break;
default:
SceneManager.LoadScene("Init");
break;
}
});
_socket.On("pauseGame", (string payload) => {
GameplayManager.getManager().ResumeGame();
Debug.Log("Unpaused");
});
_socket.On("resumeGame", (string payload) => {
GameplayManager.getManager().PauseGame();
Debug.Log("Paused");
});
_socket.On("updateClientPosition", (string payload) => {
Debug.Log(payload);
var obj = JsonConvert.DeserializeObject<Position>(payload);
float y = obj.yPos;
float x = obj.xPos;
float z = obj.zPos;
movePlayerX(x);
movePlayerY(y);
movePlayerZ(z);
});
_socket.On("kickPatient", (string payload) => {
Debug.Log(payload);
SceneManager.LoadScene("Init");
});
_socket.On("handMirror", (string payload) => {
Debug.Log(payload);
switch (payload) {
case "LEFT":
CalibrationManager.getManager().SetCalibrationType(2);
CalibrationManager.getManager().SetUnaffectedController(OVRInput.Controller.LTouch);
break;
case "RIGHT":
CalibrationManager.getManager().SetCalibrationType(2);
CalibrationManager.getManager().SetUnaffectedController(OVRInput.Controller.RTouch);
break;
default:
CalibrationManager.getManager().SetCalibrationType(0);
break;
}
});
}
private void OnDestroy()
{
_socket.Close();
}
public void SetPatientName()
{
PlayerPrefs.SetString("PatientName", patientNameInput.text);
FetchName();
}
public void SwitchToSetName()
{
setName.SetActive(true);
patientName.gameObject.SetActive(false);
}
private void SwitchToViewName()
{
setName.SetActive(false);
patientName.gameObject.SetActive(true);
}
}
}
And here is the JS server files
import express, { json } from "express";
const app = express();
import cors from "cors";
import http from "http";
const httpServer = http.createServer(app);
import { Server, Socket } from "socket.io";
import "./database";
import {
addPatient,
checkUserWithPassword,
clearToken,
deleteSession,
getAllSessions,
getSessionsByUsername,
insertSession,
removePatientFromSession,
} from "./database";
import { ISession } from "./ Models/ISession";
import { logout, validateToken } from "./auth";
import { generateUniqueId } from "./utils";
import { IUser } from "./ Models/IUser";
const port = 5000;
interface IStartGame {
sessionKey: string;
game: string;
}
const io = new Server(httpServer, {
cors: {
origin: "http://localhost:3000",
methods: ["GET", "POST"],
},
});
io.use(async (socket, next) => {
const token = socket.handshake.headers.authorization;
// const session = socket.handshake.headers.session;
/*if (verifyToken(token)) {
setTimeout(next, 1000);
}*/
next();
});
app.use(cors());
app.use(express.json());
app.use((req, res, next) => setTimeout(next, 1000));
app.post("/login", async (req, res) => {
const params = req.body as IUser;
checkUserWithPassword(params.username, params.shaPassword).then((token) => {
if (token) {
res.send({ success: true, token });
} else {
res.send({ success: false });
}
});
});
app.post("/logout", async (req, res) => {
const token = req.headers.authorization;
logout(token, () => {
res.sendStatus(200);
});
});
app.post("/loginWithToken", (req, res) => {
const token = req.headers.authorization;
validateToken(token, (success: boolean, user: IUser) => {
if (success) {
res.send({
success: true,
user,
});
} else {
res.send({ success: false });
}
});
});
app.get("/sessions", (req, res) => {
const cred = req.body.user as IUser;
const token = req.headers.authorization;
validateToken(token, (success: boolean, user: IUser) => {
if (success) {
getAllSessions()
.then((sessions) => {
res.send({ sessions });
})
.catch(() => {
res.sendStatus(500);
});
} else {
res.sendStatus(403);
}
});
});
app.post("/session", (req, res) => {
const params = req.body as ISession;
const token = req.headers.authorization;
validateToken(token, (success: boolean, user: IUser) => {
if (success) {
getAllSessions().then((sessions: ISession[]) => {
const id = generateUniqueId(
5,
sessions.map((s) => s.sessionKey)
);
insertSession({
sessionName: params.sessionName,
sessionKey: id,
createdBy: user.username,
patients: [],
}).then((suc) => {
if (suc) {
res.send({ success: true });
} else {
res.sendStatus(500);
}
});
});
} else {
res.sendStatus(403);
}
});
});
app.delete("/sessionDelete", (req, res) => {
const params = req.body;
const token = req.headers.authorization;
validateToken(token, (success: boolean) => {
if (success) {
deleteSession(params.sessionKey);
console.log(`Clinicain deleted session ${params.sessionKey}`);
res.sendStatus(200);
} else {
res.sendStatus(403);
}
});
});
const unitySockets = {};
interface IUnitySocket {
name: string;
socket: Socket;
}
const addUnitySocket = (name: string, socket: Socket) => {
unitySockets[socket.id] = {
name,
socket,
};
};
io.on("connection", (socket) => {
console.log(`Client ${socket.id} has connected`);
socket.on("disconnect", () => {
if (unitySockets[socket.id]) {
unitySockets[socket.id].socket.disconnect();
delete unitySockets[socket.id];
}
console.log(`Client ${socket.id} has disconnected`);
});
socket.on("unityConnect", (name: string) => {
console.log(name + " joined unity");
socket.join("waiting");
addUnitySocket(name, socket);
});
socket.on("unityChangeName", (name: string) => {
if (unitySockets[socket.id]) {
unitySockets[socket.id].name = name;
} else {
addUnitySocket(name, socket);
}
});
app.post("/startGame", (req, res) => {
const params = req.body as IStartGame;
console.log(params.sessionKey);
console.log(params.game);
socket.to(params.sessionKey).emit("startGame", { game: params.game });
res.sendStatus(200);
});
app.patch("/pause", (req, res) => {
const params = req.body;
console.log("Clicked pause");
if (params.isPaused) {
socket.to(params.sessionKey).emit("resumeGame");
} else {
socket.to(params.sessionKey).emit("pauseGame");
}
res.sendStatus(200);
});
app.post("/updateClientPosition", (req, res) => {
const params = req.body;
console.log(params);
socket.to(params.sessionKey).emit("updateClientPosition", params.value);
res.sendStatus(200);
});
app.post("/handMirror", (req, res) => {
const params = req.body;
socket.to(params.sessionKey).emit("handMirror", params.hand);
res.sendStatus(200);
});
app.delete("/removePatientFromSession", (req, res) => {
const params = req.body;
const token = req.headers.authorization;
validateToken(token, (success: boolean) => {
if (success) {
removePatientFromSession(params.sessionKey, params.patientId);
console.log(`Clinician kicked user ${params.patientId}`);
socket.to(params.sessionKey).emit("kickPatient", params.patientId);
res.sendStatus(200);
} else {
res.sendStatus(403);
}
});
});
app.post("/leaveAllRooms", (req, res) => {
const rooms = io.sockets.adapter.sids[socket.id];
console.log(rooms);
// tslint:disable-next-line:forin
for (const r in rooms) {
socket.leave(r);
console.log("left room " + r);
}
res.sendStatus(200);
});
app.post("/join", (req, res) => {
const params = req.body as ISession;
socket.join(params.sessionKey);
console.log(`joined room ${params.sessionKey}`);
socket.to(params.sessionKey).emit("userJoined");
socket.emit("userJoined");
res.send({ success: true });
});
app.get("/getWaitingClients", (req, res) => {
const waitingSet = io.sockets.adapter.rooms.get("waiting");
const retList = [];
console.log(waitingSet);
if (!waitingSet) {
res.send({ waitingList: [] });
} else {
const waiting = Array.from(waitingSet);
waiting.forEach((val) => {
const unitySocket: IUnitySocket = unitySockets[val];
if (unitySocket) {
retList.push({
name: unitySocket.name,
socketId: val,
});
}
});
res.send({ waitingList: retList });
}
});
app.get("/getPatientsInSession", (req, res) => {
const roomKey = req.query.roomKey.toString();
console.log("Get room " + roomKey);
const roomSet = io.sockets.adapter.rooms.get(roomKey);
if (roomSet) {
const patients = Array.from(roomSet);
const retList = [];
patients.forEach((id) => {
if (unitySockets[id]) {
retList.push({
name: unitySockets[id].name,
socketId: id,
});
}
});
res.send({ patientList: retList });
} else {
res.send({ patientList: [] });
}
});
app.post("/addClientsToSession", (req, res) => {
const clientList = req.body.clientList;
const roomKey = req.body.roomKey;
const clinicianName = req.body.clinician;
console.log("Add to room " + roomKey);
clientList.forEach((id) => {
if (unitySockets[id].socket) {
unitySockets[id].socket.leave("waiting");
unitySockets[id].socket.join(roomKey);
unitySockets[id].socket.emit("userJoined", {
clinician: clinicianName,
});
}
});
addPatient(clientList, roomKey);
res.sendStatus(200);
});
app.post("/leave", (req, res) => {
const params = req.body as ISession;
socket.leave(params.sessionKey);
console.log(`left room ${params.sessionKey}`);
socket.to(params.sessionKey).emit("userLeft");
socket.emit("userLeft");
res.send({ success: true });
});
});
httpServer.listen(port, () => {
console.log(`server is listening on ${port}`);
});
The Unity scene interface:
Unity Scence
I found how to do it, I used heroku to host my node.js server app and then had to change how the front end electron would connect to the heroku website. Same thing goes for the unity quest game.

Await inside an await doesn't give correct output

Below is my code where I want to check if a token exists. If yes, then I will check if the wallet owner is the token owner. Problem now is it doesn't check the second function "contract.methods.ownerOf(tokenId).call(function (err, res)" thus the final result is not the correct outcome.
async function doesTokenIdExist(tokenId, contract, walletAddress) {
var tokenExists = false;
await contract.methods.exists(tokenId).call(async function (err, res) {
if (res) {
await contract.methods.ownerOf(tokenId).call(function (err, res) {
if (!err) {
tokenAddress = res.toLowerCase();
walletAddress = walletAddress.toLowerCase();
if (tokenAddress.localeCompare(walletAddress) == 0){
tokenExists = true;
} else {
tokenExists = false;
}
} else {
tokenExists = false;
}
});
} else {
tokenExists = false;
}
});
return tokenExists;
}
Change this,
await contract.methods.exists(tokenId).call(function (err, res) {
to this,
await contract.methods.exists(tokenId).call(async function (err, res) {

User authorization using custom middleware in loopback

I have made VerifyUserAccess middleware in loopback as below:
module.exports = function(options) {
return function storeCurrentUser(req, res, next) {
if (!req.accessToken) {
return next();
}
app.models.UserModel.findById(req.accessToken.userId, function(err, user) {
if (err) {
return next(err);
}
if (!user) {
return next(new Error('No user with this access token was found.'));
}
const LoopBackContext = require('loopback-context');
const loopbackContext = LoopBackContext.getCurrentContext();
if (loopbackContext) {
loopbackContext.set('currentUser', user);
}
next();
});
};
};
And Also I added it in middleware.json as below:
...
"initial": {
"./middleware/verify_user_access": {},
...
}
...
But problem is that always loopbackContext I got null.
And the second question is that how to access currentUser result in API. I have done below:
Gameversionandtype.GetGameversionandtypeByGameTypeID = (ctx, callback) =>
{
const LoopBackContext = require('loopback-context');
const context = LoopBackContext.getCurrentContext();
const currentUserResult = context && context.get('currentUserResult');
console.log('currentUser.username: ', currentUserResult.id);
...
...
}
And Please let me know that how to set currentUser result and how to get it in API. I have referred documentation as well loopback-context npm module but I can not find any solutions. Thanking you in advance.

getModel() is not defined - but it was defined

I am creating file upload functionality on Cloud Storage. Here's the backend code:
const Storage = require('#google-cloud/storage')
const storage = Storage({
projectId: 'a-1485'
})
const bucket = storage.bucket('a-1485.appspot.com')
function getPublicUrl (filename) {
return 'https://storage.googleapis.com/a-1485.appspot.com/${filename}'
}
function sendUploadToGCS (req, res, next) {
if (!req.file) {
return next()
}
const gcsname = Date.now() + req.file.originalname
console.log(gcsname)
const file = bucket.file(gcsname)
const stream = file.createWriteStream({
metadata: {
contentType: req.file.mimetype
}
})
stream.on('error', (err) => {
req.file.cloudStorageError = err
next(err)
})
stream.on('finish', () => {
req.file.cloudStorageObject = gcsname
req.file.cloudStoragePublicUrl = getPublicUrl(gcsname)
next()
})
stream.end(req.file.buffer);
}
module.exports = {
getPublicUrl,
sendUploadToGCS
}
In my app.js file:
app.post('/upload-image', multer.single('image'), images.sendUploadToGCS, (req, res, next) => {
let data = req.body
console.log(data)
if (req.file && req.file.cloudStoragePublicUrl) {
data.imageUrl = req.file.cloudStoragePublicUrl
}
getModel().create(data, (err, savedData) => {
if (err) {
next(err)
return
}
res.redirect(`${req.baseUrl}/${savedData.id}`);
})
}
)
However, when I upload an image I get an error thrown saying 'getModel()' is not defined. But as you can see above it is defined. What is the problem?

Categories

Resources