Skip to content

Commit

Permalink
Add/Update Test Cases
Browse files Browse the repository at this point in the history
- For UserServiceAPI
  • Loading branch information
Jai2501 committed Nov 14, 2023
1 parent d6f5acd commit c870039
Show file tree
Hide file tree
Showing 5 changed files with 293 additions and 254 deletions.
194 changes: 97 additions & 97 deletions Frontend/src/Authentication/UserAuthenticationController.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,127 +4,127 @@ import {
signOut,
onAuthStateChanged,
sendPasswordResetEmail,
} from "firebase/auth";
import {auth} from "./firebase";
import setFirebaseUserCredentials from "./AuthenticationState";
import {setLocalUserState} from "../User/UserStateController";
var unsubscribeAuthenticationStateObserver = null;
var errorMap = {
} from "firebase/auth";

import { auth } from "./firebase";

import setFirebaseUserCredentials from "./AuthenticationState";

import { setLocalUserState } from "../User/UserStateController";

var unsubscribeAuthenticationStateObserver = null;

var errorMap = {
"auth/weak-password": "Password should be atleast 6 characters!",
"auth/invalid-email": "Please enter valid email!",
};
async function registerUserUsingFirebase(userEmail, userPassword) {
};
async function registerUserUsingFirebase(userEmail, userPassword) {
var res = {
success: false,
message: "",
success: false,
message: "",
};

try {
const userCredential = await createUserWithEmailAndPassword(
auth,
userEmail,
userPassword
);
console.log("User Created Successfully: " + userCredential.user);
res.success = true;
return res;
const userCredential = await createUserWithEmailAndPassword(
auth,
userEmail,
userPassword
);

console.log("User Created Successfully: " + userCredential.user);
res.success = true;
return res;
} catch (error) {
// Error
console.log("User Creation Unsuccessful: " + error);
res.success = false;
res.message = errorMap[error["code"]];
return res;
// Error
console.log("User Creation Unsuccessful: " + error);
res.success = false;
res.message = errorMap[error["code"]];
return res;
}
}
async function loginUserUsingFirebase(userEmail, userPassword) {
}

async function loginUserUsingFirebase(userEmail, userPassword) {
try {
const userCredential = await signInWithEmailAndPassword(
auth,
userEmail,
userPassword
);
console.log("User Logged In Successfully: " + userCredential.user);
return true;
const userCredential = await signInWithEmailAndPassword(
auth,
userEmail,
userPassword
);

console.log("User Logged In Successfully: " + userCredential.user);

return true;
} catch (error) {
console.log("User Login Unsuccessful: " + error);
// Invalid Email/Password
return false;
console.log("User Login Unsuccessful: " + error);

// Invalid Email/Password
return false;
}
}
async function logoutUserUsingFirebase() {
}

async function logoutUserUsingFirebase() {
try {
await signOut(auth);
console.log("Signout Successful");
return true;
await signOut(auth);

console.log("Signout Successful");

return true;
} catch (error) {
console.log("Could Not Signout: " + error);
return false;
console.log("Could Not Signout: " + error);

return false;
}
}
async function resetUserPasswordUsingFirebase(userEmail) {
}

async function resetUserPasswordUsingFirebase(userEmail) {
try {
await sendPasswordResetEmail(auth, userEmail);
console.log("Password Reset Email Sent Successfully");
return true;
await sendPasswordResetEmail(auth, userEmail);

console.log("Password Reset Email Sent Successfully");

return true;
} catch (error) {
console.log("Could Not Send Password Reset Email : " + error);
return false;
console.log("Could Not Send Password Reset Email : " + error);

return false;
}
}
async function isUserLoggedInUsingFirebase() {
}

async function isUserLoggedInUsingFirebase() {
return new Promise((resolve, reject) => {
const unsubscribe = onAuthStateChanged(auth, (user) => {
unsubscribe(); // Stop listening to further changes
if (user) {
resolve(true); // User is logged in
} else {
resolve(false); // User is not logged in
}
});
const unsubscribe = onAuthStateChanged(auth, (user) => {
unsubscribe(); // Stop listening to further changes
if (user) {
resolve(true); // User is logged in
} else {
resolve(false); // User is not logged in
}
});
});
}
function observeAuthState() {
}

function observeAuthState() {
if (unsubscribeAuthenticationStateObserver !== null) {
unsubscribeAuthenticationStateObserver();
unsubscribeAuthenticationStateObserver();
}

unsubscribeAuthenticationStateObserver = onAuthStateChanged(
auth,
async (user) => {
setFirebaseUserCredentials(user);
// Possible to add setUserState Here (Issue: Higher Coupling)
await setLocalUserState(user);
}
auth,
async (user) => {
setFirebaseUserCredentials(user);

// Possible to add setUserState Here (Issue: Higher Coupling)
await setLocalUserState(user);
}
);
}
observeAuthState();
export {
}

observeAuthState();

export {
registerUserUsingFirebase,
loginUserUsingFirebase,
logoutUserUsingFirebase,
resetUserPasswordUsingFirebase,
isUserLoggedInUsingFirebase,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ test("Test User Registration With Correct Credentials", () => {
).then((data) => {
console.log("> Register With Correct Credentials: " + data);

expect(data).toBe(true);
expect(data.success).toBe(true);
});
});

Expand All @@ -35,7 +35,7 @@ test("Test User Registration With Incorrect Credentials", () => {
(data) => {
console.log("> Register With Incorrect Credentials: " + data);

expect(data).toBe(false);
expect(data.success).toBe(false);
}
);
});
Expand All @@ -47,7 +47,7 @@ test("Test User Registration With Same Duplicate Email", () => {
).then((data) => {
console.log("> Register With Duplicate Email: " + data);

expect(data).toBe(false);
expect(data.success).toBe(false);
});
});

Expand Down
41 changes: 40 additions & 1 deletion Frontend/src/User/UnitTests/UserServiceAPI.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ const {
isUserLoggedIn,
} = require("../../Authentication/AuthenticationState");

afterAll(async () => {
await deleteUser();
});

test("Test User Registration Flow", async () => {
return registerUser(
"Test Name",
Expand All @@ -33,11 +37,46 @@ test("Test User Registration Flow", async () => {
"TestGitHubId",
"TestLanguage"
).then(async (data) => {
expect(data).toBe(true);
expect(data.success).toBe(true);

expect(await isUserLoggedIn()).toBe(true);
expect(getUserName()).toBe("Test Name");

expect(getFirebaseUserCredentials).not.toBe(null);
});
});

test("Test User Logout Flow", async () => {
return logoutUser().then((data) => {
expect(data).toBe(true);
});
});

test("Test User Login Flow", async () => {
return loginUser("[email protected]", "Password123").then(async (data) => {
expect(data).toBe(true);

expect(getUserName()).toBe("Test Name");
expect(getUserEmail()).toBe("[email protected]");
expect(getUserPreferredLanguage()).toBe("TestLanguage");
expect(getUserGithubId()).toBe("TestGitHubId");
expect(await isUserAdmin()).toBe(false);
});
});

test("Test User Data Update Flow", async () => {
return updateUserData(
"Updated Name",
"[email protected]",
"UpdatedGitHubId",
"UpdatedTestLanguage"
).then(async (data) => {
expect(data).toBe(true);

expect(getUserName()).toBe("Updated Name");
expect(getUserEmail()).toBe("[email protected]");
expect(getUserPreferredLanguage()).toBe("UpdatedTestLanguage");
expect(getUserGithubId()).toBe("UpdatedGitHubId");
expect(await isUserAdmin()).toBe(false);
});
});
Loading

0 comments on commit c870039

Please sign in to comment.