Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
rrd108
committed
Mar 8, 2024
1 parent
a6149e4
commit 2dc94f7
Showing
8 changed files
with
64 additions
and
2 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
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,31 @@ | ||
import { describe, it, expect } from "vitest"; | ||
import { fileURLToPath } from "node:url"; | ||
import { setup, $fetch } from "@nuxt/test-utils/e2e"; | ||
|
||
describe("middleware", async () => { | ||
await setup({ | ||
rootDir: fileURLToPath(new URL("./fixtures/bearer", import.meta.url)), | ||
}); | ||
|
||
it("deny access with an invalid token", async () => { | ||
try { | ||
const response = await $fetch("/api/users", { | ||
method: "GET", | ||
headers: { authorization: "invalidTestToken" }, | ||
}); | ||
expect(true).toBe(false); | ||
} catch (err) { | ||
const typedErr = err as { statusCode: number; statusMessage: string }; | ||
expect(typedErr.statusCode).toBe(401); | ||
expect(typedErr.statusMessage).toBe("Authentication error"); | ||
} | ||
}); | ||
|
||
it("allow access with valid bearer token", async () => { | ||
const response = await $fetch("/api/users", { | ||
method: "GET", | ||
headers: { authorization: "Bearer 270fsdg04%rt2f6$)b4eblok0dfgauranga" }, | ||
}); | ||
expect(response.results[0].name).toBe("Gauranga"); | ||
}); | ||
}); |
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,6 @@ | ||
<template> | ||
<div>basic</div> | ||
</template> | ||
|
||
<script setup> | ||
</script> |
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,10 @@ | ||
import NuxtTokenAuthentication from "../../../src/module"; | ||
|
||
export default defineNuxtConfig({ | ||
modules: [NuxtTokenAuthentication], | ||
nuxtTokenAuthentication: { | ||
noAuthRoutes: ["GET:/api/route_noauth"], | ||
tokenHeader: "Authorization", | ||
prefix: "Bearer", | ||
}, | ||
}); |
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,5 @@ | ||
{ | ||
"private": true, | ||
"name": "basic", | ||
"type": "module" | ||
} |
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,3 @@ | ||
export default defineEventHandler(async (event) => { | ||
return { result: "Gauranga" }; | ||
}); |
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,3 @@ | ||
export default defineEventHandler(async (event) => { | ||
return { results: [{ id: 1, name: "Gauranga" }] }; | ||
}); |