-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
652e73f
commit a5f7e88
Showing
23 changed files
with
4,655 additions
and
0 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,3 @@ | ||
node_modules | ||
dist | ||
coverage |
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,16 @@ | ||
{ | ||
"root": true, | ||
"env": { | ||
"browser": true, | ||
"node": true, | ||
"jest/globals": true | ||
}, | ||
"parser": "@typescript-eslint/parser", | ||
"plugins": ["@typescript-eslint", "jest"], | ||
"extends": ["eslint:recommended", "plugin:@typescript-eslint/eslint-recommended", "plugin:@typescript-eslint/recommended"], | ||
"rules": { | ||
"no-console": 0, | ||
"@typescript-eslint/no-explicit-any": ["off"], | ||
"no-unsafe-finally": "off" | ||
} | ||
} |
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 @@ | ||
* text=auto eol=lf |
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,24 @@ | ||
name: Unit Tests | ||
|
||
on: [pull_request, push] | ||
|
||
jobs: | ||
tests: | ||
runs-on: ubuntu-latest | ||
|
||
strategy: | ||
matrix: | ||
node-version: [18.x, 16.x] | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Use Node.js ${{ matrix.node-version }} | ||
uses: actions/setup-node@v1 | ||
with: | ||
node-version: ${{ matrix.node-version }} | ||
- name: pnpm install global | ||
run: npm i -g pnpm | ||
- name: deps install | ||
run: pnpm i | ||
- name: tests | ||
run: pnpm test |
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,11 @@ | ||
# packages | ||
node_modules/**/* | ||
|
||
# production build | ||
dist | ||
|
||
# log files | ||
*.log | ||
|
||
# environment variables | ||
.env |
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,4 @@ | ||
{ | ||
"tabWidth": 4, | ||
"printWidth": 230 | ||
} |
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,59 @@ | ||
import request from "supertest"; | ||
import app from "../src/index"; | ||
|
||
describe("Auth API Tests", () => { | ||
let authToken = ""; | ||
let testUserId = ""; | ||
|
||
// Test user registration | ||
it("registers a user", (done: jest.DoneCallback) => { | ||
request(app) | ||
.post("/api/v1/register") | ||
.send({ username: "testuser", password: "testpassword" }) | ||
.expect(200) | ||
.expect((res) => { | ||
expect(res.body.msg).toBe("Successfully registered User"); | ||
testUserId = res.body.data.id; | ||
}) | ||
.end(done); | ||
}); | ||
|
||
// Test user login and JWT generation | ||
it("logs in a user and generates a JWT", (done) => { | ||
request(app) | ||
.post("/api/v1/login") | ||
.send({ username: "testuser", password: "testpassword" }) | ||
.expect(200) | ||
.expect((res) => { | ||
expect(res.body.data.token).toBeDefined(); | ||
authToken = res.body.data.token; | ||
}) | ||
.end(done); | ||
}); | ||
|
||
// Test access to a protected resource | ||
it("allows access to a protected resource with a valid JWT", (done) => { | ||
request(app) | ||
.get("/api/v1/protected") | ||
.set("Authorization", `Bearer ${authToken}`) | ||
.expect(200) | ||
.expect((res) => expect(res.body.msg).toBe("You are authorized to access this resource.")) | ||
.end(done); | ||
}); | ||
|
||
// Test access to a protected resource without a JWT | ||
it("blocks access to a protected resource without a JWT", (done) => { | ||
request(app) | ||
.get("/api/v1/protected") | ||
.expect(401) | ||
.expect((res) => expect(res.text).toBe("Unauthorized")) | ||
.end(done); | ||
}); | ||
|
||
// Clean up after testing | ||
afterAll((done) => { | ||
if (testUserId) { | ||
request(app).delete(`/api/v1/user/${testUserId}`).set("Authorization", `Bearer ${authToken}`).expect(200).end(done); | ||
} | ||
}); | ||
}); |
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,12 @@ | ||
module.exports = { | ||
transform: { | ||
"^.+\\.ts?$": "ts-jest", | ||
}, | ||
testEnvironment: "node", | ||
testMatch: ["**/**/*.test.ts"], | ||
verbose: true, | ||
forceExit: true, | ||
clearMocks: true, | ||
resetMocks: true, | ||
restoreMocks: true, | ||
}; |
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,46 @@ | ||
{ | ||
"name": "sso-server", | ||
"version": "1.0.0", | ||
"main": "dist/index.js", | ||
"scripts": { | ||
"start": "tsc && node dist/index.js", | ||
"dev": "nodemon src/index.ts", | ||
"lint": "eslint . --ext .ts", | ||
"lint-and-fix": "eslint . --ext .ts --fix", | ||
"build": "tsc", | ||
"test": "jest", | ||
"test:dev": "jest --watchAll" | ||
}, | ||
"license": "MIT", | ||
"dependencies": { | ||
"bcryptjs": "^2.4.3", | ||
"dotenv": "^16.3.1", | ||
"express": "^4.18.2", | ||
"jsonwebtoken": "^9.0.2", | ||
"mongoose": "^7.6.3", | ||
"passport": "^0.6.0", | ||
"passport-jwt": "^4.0.1", | ||
"passport-local": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"@types/bcryptjs": "^2.4.5", | ||
"@types/express": "^4.17.20", | ||
"@types/jest": "^29.5.6", | ||
"@types/jsonwebtoken": "^9.0.4", | ||
"@types/node": "^20.8.7", | ||
"@types/passport": "^1.0.14", | ||
"@types/passport-jwt": "^3.0.11", | ||
"@types/passport-local": "^1.0.37", | ||
"@types/supertest": "^2.0.15", | ||
"@typescript-eslint/eslint-plugin": "^6.9.0", | ||
"@typescript-eslint/parser": "^6.9.0", | ||
"eslint": "^8.52.0", | ||
"eslint-plugin-jest": "^27.4.3", | ||
"jest": "^29.7.0", | ||
"nodemon": "^3.0.1", | ||
"supertest": "^6.3.3", | ||
"ts-jest": "^29.1.1", | ||
"ts-node": "^10.9.1", | ||
"typescript": "^5.2.2" | ||
} | ||
} |
Oops, something went wrong.