Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test suite setup + Code quality automation #90

Merged
merged 3 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
module.exports = {
parser: "@typescript-eslint/parser",
plugins: [
"@typescript-eslint"
],
plugins: ["@typescript-eslint"],
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
"plugin:@typescript-eslint/recommended",
],
env: {
node: true
node: true,
},
rules: {
"no-console": ["error", {"allow": ["warn"]}]
}
}
"no-console": ["error", { allow: ["warn"] }],
},
};
43 changes: 43 additions & 0 deletions .github/workflows/code-quality.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Code Quality
on: [push]

concurrency: ${{ github.workflow }}-${{ github.ref }}

env:
CI: true

jobs:
publish-packages:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Configure CI Git User
run: |
git config --global user.name 'Rajat Saxena'
git config --global user.email 'hi@sub.rajatsaxena.dev'
git remote set-url origin https://$GITHUB_ACTOR:$GITHUB_PAT@github.com/codelitdev/medialit
env:
GITHUB_PAT: ${{ secrets.PAT }}

- name: Checkout and pull branch
run: git checkout "${GITHUB_REF:11}" && git pull

- name: Install pnpm
run: |
npm i pnpm@latest -g

- name: Install dependencies
run: pnpm install

- name: Run lint
run: pnpm lint

- name: Run prettier
run: pnpm prettier

- name: Run test
run: pnpm test
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
dist
dist
**/.next
44 changes: 44 additions & 0 deletions apps/api/__tests__/apikey/handlers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import test, { afterEach, describe, mock } from "node:test";
import { createApikey } from "../../lib/apikey/handlers";
import assert from "node:assert";
import queries from "../../lib/apikey/queries";

describe("API key test suite", () => {
afterEach(() => {
mock.restoreAll();
});

test("Create API key throws an error if name is empty", async (t) => {
const req = {
body: {},
};
const res = {
status: () => ({
json: (data: any) => data,
}),
};
const response = await createApikey(req, res, () => {}); // eslint-disable-line @typescript-eslint/no-empty-function
assert.strictEqual(response.error, "Name is required");
});

test("Create API succeeds if name is provided", async (t) => {
const req = {
body: {
name: "Test API",
},
user: {
id: "123",
},
};
const res = {
status: () => ({
json: (data: any) => data,
}),
};
mock.method(queries, "createApiKey").mock.mockImplementation(
async () => ({ key: "123" })
);
const response = await createApikey(req, res, () => {}); // eslint-disable-line @typescript-eslint/no-empty-function
assert.strictEqual(response.key, "123");
});
});
7 changes: 0 additions & 7 deletions apps/api/__tests__/core.test.js

This file was deleted.

4 changes: 2 additions & 2 deletions apps/api/lib/apikey/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logger from "../services/log";
import { NOT_FOUND, SUCCESS } from "../config/strings";
import { createApiKey, deleteApiKey, getApiKeyByUserId } from "./queries";
import queries, { deleteApiKey, getApiKeyByUserId } from "./queries";
import { Apikey } from "@medialit/models";

export async function createApikey(
Expand All @@ -14,7 +14,7 @@ export async function createApikey(
}

try {
const apikey: Apikey = await createApiKey(req.user.id, name);
const apikey: Apikey = await queries.createApiKey(req.user.id, name);

return res.status(200).json({
key: apikey.key,
Expand Down
7 changes: 7 additions & 0 deletions apps/api/lib/apikey/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,10 @@ export async function deleteApiKey(
userId,
});
}

export default {
createApiKey,
getApiKeyUsingKeyId,
getApiKeyByUserId,
deleteApiKey,
};
98 changes: 0 additions & 98 deletions apps/api/lib/user/__test__/queries.test.js
Original file line number Diff line number Diff line change
@@ -1,98 +0,0 @@
const test = require("node:test");
const assert = require("node:assert/strict");
const { getUser } = require("../queries"); // Adjust the path accordingly
const UserModel = require("../model"); // Import your actual user model

// Mock UserModel
const mockUserModel = {
findById: async (id) => {
// Mock user object with the specified fields
return {
_id: id,
email: "mock@example.com",
active: true,
name: "Mock User",
};
},
};

test("should return a valid userId", async (t) => {
const validUserId = "12345abcd";

const user = await mockUserModel.findById(validUserId);

const result = await getUser(validUserId);
assert.strictEqual(typeof result.id, "string", "UserId should be a string");
assert.deepStrictEqual(result.id, user.id);
});

test("should return a invalid userId", async (t) => {
const validUserId = "12345efgh";

const user = await mockUserModel.findById(validUserId);

const result = await getUser(validUserId);
// assert.strictEqual(retrievedUser.id, existingUserId);
assert.notDeepStrictEqual(result.id, user.id);
});

// describe('getUser function', () => {
// it('should return a user when a valid ID is provided', async () => {
// // Arrange
// const userId = 'validUserId';

// // Mock the findById method
// const originalFindById = UserModel.findById;
// UserModel.findById = async (id) => mockUserModel.findById(id);

// // Act
// const result = await getUser(userId);

// // Assert
// assert.deepStrictEqual(result, {
// _id: userId,
// email: 'mock@example.com',
// active: true,
// name: 'Mock User',
// });

// // Restore the original method
// UserModel.findById = originalFindById;
// });

// it('should return null when an invalid ID is provided', async () => {
// // Arrange
// const invalidUserId = 'invalidUserId';

// // Mock the findById method
// const originalFindById = UserModel.findById;
// UserModel.findById = async (id) => null;

// // Act
// const result = await getUser(invalidUserId);

// // Assert
// assert.strictEqual(result, null);

// // Restore the original method
// UserModel.findById = originalFindById;
// });

// it('should handle errors and reject the promise', async () => {
// // Arrange
// const errorUserId = 'errorUserId';
// const errorMessage = 'An error occurred';

// // Mock the findById method
// const originalFindById = UserModel.findById;
// UserModel.findById = async (id) => {
// throw new Error(errorMessage);
// };

// // Act & Assert
// await assert.rejects(async () => await getUser(errorUserId), { message: errorMessage });

// // Restore the original method
// UserModel.findById = originalFindById;
// });
// });
4 changes: 3 additions & 1 deletion apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"scripts": {
"build": "tsc",
"dev": "nodemon lib/index.ts",
"start": "node dist/index.js"
"start": "node dist/index.js",
"test": "node --import tsx --test **/*.test.ts"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.55.0",
Expand Down Expand Up @@ -67,6 +68,7 @@
"eslint": "^8.12.0",
"nodemon": "^2.0.15",
"ts-node": "^10.7.0",
"tsx": "^4.7.0",
"typescript": "^5.2.2"
}
}
10 changes: 4 additions & 6 deletions apps/web/app/dashboard/app/[name]/files/loading.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import React from 'react'
import React from "react";

const Loading = () => {
return (
<div>Loading...</div>
)
}
return <div>Loading...</div>;
};

export default Loading
export default Loading;
6 changes: 1 addition & 5 deletions apps/web/app/dashboard/app/[name]/tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ import React from "react";
import Link from "next/link";
import { usePathname } from "next/navigation";

export default async function Tabs({
params,
}: {
params: { name: string };
}) {
export default async function Tabs({ params }: { params: { name: string } }) {
const name = params.name;
const currentPathName = usePathname();

Expand Down
10 changes: 4 additions & 6 deletions apps/web/app/dashboard/loading.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import React from 'react'
import React from "react";

const Loading = () => {
return (
<div>Loading...</div>
)
}
return <div>Loading...</div>;
};

export default Loading
export default Loading;
Loading
Loading