Skip to content

Commit

Permalink
feat: init
Browse files Browse the repository at this point in the history
  • Loading branch information
belphegor-s committed Oct 23, 2023
1 parent 652e73f commit a5f7e88
Show file tree
Hide file tree
Showing 23 changed files with 4,655 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
dist
coverage
16 changes: 16 additions & 0 deletions .eslintrc
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"
}
}
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
24 changes: 24 additions & 0 deletions .github/workflows/testing.yaml
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
11 changes: 11 additions & 0 deletions .gitignore
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
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"tabWidth": 4,
"printWidth": 230
}
59 changes: 59 additions & 0 deletions __tests__/index.test.ts
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);
}
});
});
12 changes: 12 additions & 0 deletions jest.config.js
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,
};
46 changes: 46 additions & 0 deletions package.json
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"
}
}
Loading

0 comments on commit a5f7e88

Please sign in to comment.