-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
40 send jwt as part of auth header in request (#44)
- Loading branch information
Showing
6 changed files
with
110 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import axios from "axios"; | ||
import MockAdapter from "axios-mock-adapter"; | ||
import { sendOTP } from "../apiService"; | ||
|
||
describe("sendOTP", () => { | ||
let mock: MockAdapter; // Explicitly declare the type | ||
|
||
beforeEach(() => { | ||
mock = new MockAdapter(axios); | ||
}); | ||
|
||
afterEach(() => { | ||
mock.restore(); | ||
}); | ||
|
||
it("should send OTP successfully with valid inputs", async () => { | ||
const fullPhoneNumber = "1234567890"; | ||
const jwtToken = "valid-token"; | ||
|
||
mock | ||
.onPost("http://localhost:8080/api/registration") | ||
.reply(200, { message: "OTP sent" }); | ||
|
||
const response = await sendOTP(fullPhoneNumber, jwtToken); | ||
|
||
expect(response).toEqual({ message: "OTP sent" }); | ||
}); | ||
|
||
it("should throw an error when API responds with an error", async () => { | ||
const fullPhoneNumber = "1234567890"; | ||
const jwtToken = "valid-token"; | ||
|
||
mock.onPost("http://localhost:8080/api/registration").reply(500); | ||
|
||
await expect(sendOTP(fullPhoneNumber, jwtToken)).rejects.toThrow( | ||
"Failed to send OTP", | ||
); | ||
}); | ||
|
||
it("should throw an error when an invalid JWT token is provided", async () => { | ||
const fullPhoneNumber = "1234567890"; | ||
const jwtToken = "invalid-token"; | ||
|
||
mock.onPost("http://localhost:8080/api/registration").reply(401); | ||
|
||
await expect(sendOTP(fullPhoneNumber, jwtToken)).rejects.toThrow( | ||
"Failed to send OTP", | ||
); | ||
}); | ||
|
||
it("should handle empty phone number and JWT token", async () => { | ||
await expect(sendOTP("", "")).rejects.toThrow("Failed to send OTP"); | ||
}); | ||
|
||
it("should handle network error", async () => { | ||
const fullPhoneNumber = "1234567890"; | ||
const jwtToken = "valid-token"; | ||
|
||
mock.onPost("http://localhost:8080/api/registration").networkError(); | ||
|
||
await expect(sendOTP(fullPhoneNumber, jwtToken)).rejects.toThrow( | ||
"Failed to send OTP", | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import axios from "axios"; | ||
export const sendOTP = async (fullPhoneNumber: string, jwtToken: string) => { | ||
// Create the request object with both phone number and public key | ||
const requestBody = { | ||
phoneNumber: fullPhoneNumber, | ||
}; | ||
const headers = { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${jwtToken}`, | ||
}; | ||
|
||
try { | ||
const response = await axios.post( | ||
"http://localhost:8080/api/registration", | ||
requestBody, | ||
{ headers }, | ||
); | ||
|
||
// Log the response from the backend | ||
console.log("Response from backend:", response.data); | ||
|
||
return response.data; | ||
} catch (error) { | ||
console.error("Error sending OTP:", error); | ||
throw new Error("Failed to send OTP"); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters