Skip to content

Commit c870039

Browse files
committed
Add/Update Test Cases
- For UserServiceAPI
1 parent d6f5acd commit c870039

File tree

5 files changed

+293
-254
lines changed

5 files changed

+293
-254
lines changed

Frontend/src/Authentication/UserAuthenticationController.js

Lines changed: 97 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -4,127 +4,127 @@ import {
44
signOut,
55
onAuthStateChanged,
66
sendPasswordResetEmail,
7-
} from "firebase/auth";
8-
9-
import {auth} from "./firebase";
10-
11-
import setFirebaseUserCredentials from "./AuthenticationState";
12-
13-
import {setLocalUserState} from "../User/UserStateController";
14-
15-
var unsubscribeAuthenticationStateObserver = null;
16-
17-
var errorMap = {
7+
} from "firebase/auth";
8+
9+
import { auth } from "./firebase";
10+
11+
import setFirebaseUserCredentials from "./AuthenticationState";
12+
13+
import { setLocalUserState } from "../User/UserStateController";
14+
15+
var unsubscribeAuthenticationStateObserver = null;
16+
17+
var errorMap = {
1818
"auth/weak-password": "Password should be atleast 6 characters!",
1919
"auth/invalid-email": "Please enter valid email!",
20-
};
21-
async function registerUserUsingFirebase(userEmail, userPassword) {
20+
};
21+
async function registerUserUsingFirebase(userEmail, userPassword) {
2222
var res = {
23-
success: false,
24-
message: "",
23+
success: false,
24+
message: "",
2525
};
26-
26+
2727
try {
28-
const userCredential = await createUserWithEmailAndPassword(
29-
auth,
30-
userEmail,
31-
userPassword
32-
);
33-
34-
console.log("User Created Successfully: " + userCredential.user);
35-
res.success = true;
36-
return res;
28+
const userCredential = await createUserWithEmailAndPassword(
29+
auth,
30+
userEmail,
31+
userPassword
32+
);
33+
34+
console.log("User Created Successfully: " + userCredential.user);
35+
res.success = true;
36+
return res;
3737
} catch (error) {
38-
// Error
39-
console.log("User Creation Unsuccessful: " + error);
40-
res.success = false;
41-
res.message = errorMap[error["code"]];
42-
return res;
38+
// Error
39+
console.log("User Creation Unsuccessful: " + error);
40+
res.success = false;
41+
res.message = errorMap[error["code"]];
42+
return res;
4343
}
44-
}
45-
46-
async function loginUserUsingFirebase(userEmail, userPassword) {
44+
}
45+
46+
async function loginUserUsingFirebase(userEmail, userPassword) {
4747
try {
48-
const userCredential = await signInWithEmailAndPassword(
49-
auth,
50-
userEmail,
51-
userPassword
52-
);
53-
54-
console.log("User Logged In Successfully: " + userCredential.user);
55-
56-
return true;
48+
const userCredential = await signInWithEmailAndPassword(
49+
auth,
50+
userEmail,
51+
userPassword
52+
);
53+
54+
console.log("User Logged In Successfully: " + userCredential.user);
55+
56+
return true;
5757
} catch (error) {
58-
console.log("User Login Unsuccessful: " + error);
59-
60-
// Invalid Email/Password
61-
return false;
58+
console.log("User Login Unsuccessful: " + error);
59+
60+
// Invalid Email/Password
61+
return false;
6262
}
63-
}
64-
65-
async function logoutUserUsingFirebase() {
63+
}
64+
65+
async function logoutUserUsingFirebase() {
6666
try {
67-
await signOut(auth);
68-
69-
console.log("Signout Successful");
70-
71-
return true;
67+
await signOut(auth);
68+
69+
console.log("Signout Successful");
70+
71+
return true;
7272
} catch (error) {
73-
console.log("Could Not Signout: " + error);
74-
75-
return false;
73+
console.log("Could Not Signout: " + error);
74+
75+
return false;
7676
}
77-
}
78-
79-
async function resetUserPasswordUsingFirebase(userEmail) {
77+
}
78+
79+
async function resetUserPasswordUsingFirebase(userEmail) {
8080
try {
81-
await sendPasswordResetEmail(auth, userEmail);
82-
83-
console.log("Password Reset Email Sent Successfully");
84-
85-
return true;
81+
await sendPasswordResetEmail(auth, userEmail);
82+
83+
console.log("Password Reset Email Sent Successfully");
84+
85+
return true;
8686
} catch (error) {
87-
console.log("Could Not Send Password Reset Email : " + error);
88-
89-
return false;
87+
console.log("Could Not Send Password Reset Email : " + error);
88+
89+
return false;
9090
}
91-
}
92-
93-
async function isUserLoggedInUsingFirebase() {
91+
}
92+
93+
async function isUserLoggedInUsingFirebase() {
9494
return new Promise((resolve, reject) => {
95-
const unsubscribe = onAuthStateChanged(auth, (user) => {
96-
unsubscribe(); // Stop listening to further changes
97-
if (user) {
98-
resolve(true); // User is logged in
99-
} else {
100-
resolve(false); // User is not logged in
101-
}
102-
});
95+
const unsubscribe = onAuthStateChanged(auth, (user) => {
96+
unsubscribe(); // Stop listening to further changes
97+
if (user) {
98+
resolve(true); // User is logged in
99+
} else {
100+
resolve(false); // User is not logged in
101+
}
102+
});
103103
});
104-
}
105-
106-
function observeAuthState() {
104+
}
105+
106+
function observeAuthState() {
107107
if (unsubscribeAuthenticationStateObserver !== null) {
108-
unsubscribeAuthenticationStateObserver();
108+
unsubscribeAuthenticationStateObserver();
109109
}
110-
110+
111111
unsubscribeAuthenticationStateObserver = onAuthStateChanged(
112-
auth,
113-
async (user) => {
114-
setFirebaseUserCredentials(user);
115-
116-
// Possible to add setUserState Here (Issue: Higher Coupling)
117-
await setLocalUserState(user);
118-
}
112+
auth,
113+
async (user) => {
114+
setFirebaseUserCredentials(user);
115+
116+
// Possible to add setUserState Here (Issue: Higher Coupling)
117+
await setLocalUserState(user);
118+
}
119119
);
120-
}
121-
122-
observeAuthState();
123-
124-
export {
120+
}
121+
122+
observeAuthState();
123+
124+
export {
125125
registerUserUsingFirebase,
126126
loginUserUsingFirebase,
127127
logoutUserUsingFirebase,
128128
resetUserPasswordUsingFirebase,
129129
isUserLoggedInUsingFirebase,
130-
};
130+
};

Frontend/src/Authentication/unit_tests/UserAuthenticationController.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ test("Test User Registration With Correct Credentials", () => {
2626
).then((data) => {
2727
console.log("> Register With Correct Credentials: " + data);
2828

29-
expect(data).toBe(true);
29+
expect(data.success).toBe(true);
3030
});
3131
});
3232

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

38-
expect(data).toBe(false);
38+
expect(data.success).toBe(false);
3939
}
4040
);
4141
});
@@ -47,7 +47,7 @@ test("Test User Registration With Same Duplicate Email", () => {
4747
).then((data) => {
4848
console.log("> Register With Duplicate Email: " + data);
4949

50-
expect(data).toBe(false);
50+
expect(data.success).toBe(false);
5151
});
5252
});
5353

Frontend/src/User/UnitTests/UserServiceAPI.test.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ const {
2525
isUserLoggedIn,
2626
} = require("../../Authentication/AuthenticationState");
2727

28+
afterAll(async () => {
29+
await deleteUser();
30+
});
31+
2832
test("Test User Registration Flow", async () => {
2933
return registerUser(
3034
"Test Name",
@@ -33,11 +37,46 @@ test("Test User Registration Flow", async () => {
3337
"TestGitHubId",
3438
"TestLanguage"
3539
).then(async (data) => {
36-
expect(data).toBe(true);
40+
expect(data.success).toBe(true);
3741

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

4145
expect(getFirebaseUserCredentials).not.toBe(null);
4246
});
4347
});
48+
49+
test("Test User Logout Flow", async () => {
50+
return logoutUser().then((data) => {
51+
expect(data).toBe(true);
52+
});
53+
});
54+
55+
test("Test User Login Flow", async () => {
56+
return loginUser("[email protected]", "Password123").then(async (data) => {
57+
expect(data).toBe(true);
58+
59+
expect(getUserName()).toBe("Test Name");
60+
expect(getUserEmail()).toBe("[email protected]");
61+
expect(getUserPreferredLanguage()).toBe("TestLanguage");
62+
expect(getUserGithubId()).toBe("TestGitHubId");
63+
expect(await isUserAdmin()).toBe(false);
64+
});
65+
});
66+
67+
test("Test User Data Update Flow", async () => {
68+
return updateUserData(
69+
"Updated Name",
70+
71+
"UpdatedGitHubId",
72+
"UpdatedTestLanguage"
73+
).then(async (data) => {
74+
expect(data).toBe(true);
75+
76+
expect(getUserName()).toBe("Updated Name");
77+
expect(getUserEmail()).toBe("[email protected]");
78+
expect(getUserPreferredLanguage()).toBe("UpdatedTestLanguage");
79+
expect(getUserGithubId()).toBe("UpdatedGitHubId");
80+
expect(await isUserAdmin()).toBe(false);
81+
});
82+
});

0 commit comments

Comments
 (0)