-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#130] Create test_jwt_handler & fix cookies handler on client side
- Loading branch information
1 parent
df97e58
commit bb32ba9
Showing
4 changed files
with
59 additions
and
13 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
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,41 @@ | ||
from datetime import timedelta | ||
from utils.jwt_handler import create_jwt_token, verify_jwt_token | ||
|
||
|
||
def test_create_jwt_token_with_timedelta(): | ||
res = create_jwt_token( | ||
{ | ||
"uid": 1000, | ||
"uphone_number": "+628123456789", | ||
}, | ||
expires_delta=timedelta(days=1), | ||
) | ||
assert res is not None | ||
|
||
|
||
def test_create_jwt_token_without_timedelta(): | ||
res = create_jwt_token( | ||
{ | ||
"uid": 1000, | ||
"uphone_number": "+628123456789", | ||
}, | ||
) | ||
assert res is not None | ||
|
||
|
||
def test_verify_jwt_token(): | ||
token = create_jwt_token( | ||
{ | ||
"uid": 1000, | ||
"uphone_number": "+628123456789", | ||
}, | ||
expires_delta=timedelta(days=1), | ||
) | ||
assert token is not None | ||
|
||
res = verify_jwt_token(token=token) | ||
assert res == { | ||
"uid": 1000, | ||
"uphone_number": "+628123456789", | ||
"exp": res.get("exp"), | ||
} |
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 |
---|---|---|
@@ -1,16 +1,21 @@ | ||
"use server"; | ||
|
||
import { cookies } from "next/headers"; | ||
|
||
export const setCookie = (name, value) => { | ||
cookies().set(name, value); | ||
export const setCookie = (name, value, days = 2) => { | ||
const expires = new Date( | ||
Date.now() + days * 24 * 60 * 60 * 1000 | ||
).toUTCString(); | ||
document.cookie = `${name}=${value}; expires=${expires}; path=/; SameSite=Strict; Secure;`; | ||
}; | ||
|
||
export const deleteCookie = (name) => { | ||
cookies().delete(name); | ||
export const getCookie = (name) => { | ||
const cookieArr = document.cookie.split(";"); | ||
for (let i = 0; i < cookieArr.length; i++) { | ||
const cookiePair = cookieArr[i].split("="); | ||
if (name === cookiePair[0].trim()) { | ||
return decodeURIComponent(cookiePair[1]); | ||
} | ||
} | ||
return null; | ||
}; | ||
|
||
export const getCookie = (name) => { | ||
const cookie = cookies().get(name); | ||
return cookie ? cookie.value : null; | ||
export const deleteCookie = (name) => { | ||
document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; SameSite=Strict;`; | ||
}; |