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 cases merge #105

Open
wants to merge 15 commits into
base: qa
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ ENABLE_TRUST_PROXY=true
In order to interact with the API, you need to have an API key. To create one, execute the following command.

```sh
docker exec <container_id> dist/scripts/create-local-user.js
docker exec <container_id | container_name> node /app/apps/api/dist/scripts/create-local-user.js <email>
```

After running the above command, you will get an API key which you can use to interact with the app.
Expand Down Expand Up @@ -54,3 +54,9 @@ pnpm --filter=@medialit/thumbnail build
```bash
pnpm --filter=@medialit/api dev
```

### Publishing a new version

```bash
npx changeset
```
193 changes: 186 additions & 7 deletions apps/api/__tests__/apikey/handlers.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
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";
import mongoose from "mongoose";
import {
createApikey,
deleteApikey,
getApikey,
} from "../../src/apikey/handlers";
import queries from "../../src/apikey/queries";
queries;
import { NOT_FOUND, SUCCESS } from "../../src/config/strings";

describe("API key test suite", () => {
afterEach(() => {
Expand All @@ -17,7 +24,9 @@ describe("API key test suite", () => {
json: (data: any) => data,
}),
};
const response = await createApikey(req, res, () => {}); // eslint-disable-line @typescript-eslint/no-empty-function
const response = await createApikey(req, res, () => {
return 1;
}); // eslint-disable-line @typescript-eslint/no-empty-function
assert.strictEqual(response.error, "Name is required");
});

Expand All @@ -31,14 +40,184 @@ describe("API key test suite", () => {
},
};
const res = {
status: () => ({
json: (data: any) => data,
status: (code: number) => ({
json: (data: any) => ({ data, code }),
}),
};
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");

const response = await createApikey(req, res, () => {
return 1;
});
assert.strictEqual(response.data.key, "123");
assert.strictEqual(response.code, 200);
});

test("Create API throws an error", async (t) => {
const req = {
body: {
name: "Test API",
},
user: {
id: "123",
},
};
const res = {
status: (code: number) => ({
json: (data: any) => ({ data, code }),
}),
};
mock.method(queries, "createApiKey").mock.mockImplementation(
async () => {
throw new Error("Error in creating");
}
);

const response = await createApikey(req, res, () => {
return 1;
});
assert.strictEqual(response.data.error, "Error in creating");
assert.strictEqual(response.code, 500);
});

test("Get API key returns 404 if key is not found", async (t) => {
const req = {
params: {
keyId: "existentkey",
},
user: {
id: "123",
},
};
const res = {
status: (code: number) => ({
json: (data: any) => ({ data, code }),
}),
};
mock.method(queries, "getApiKeyByUserId").mock.mockImplementation(
async () => null
);

const response = await getApikey(req, res, () => {
return 1;
});
assert.strictEqual(response.data.error, NOT_FOUND);
assert.strictEqual(response.code, 404);
});

test("Get API key returns the key if found", async (t) => {
const req = {
params: {
keyId: "M8purwS5KWwdYMHKD7XBz",
},
user: {
id: new mongoose.Types.ObjectId("652e67318ff18780e2eb5bf5"),
},
};
const res = {
status: () => ({
json: (data: any) => data,
}),
};

const apikey = {
name: "key 9",
key: "M8purwS5KWwdYMHKD7XBz",
httpReferrers: [],
ipAddresses: [],
internal: false,
createdAt: new Date("2024-01-25T10:21:48.141Z"),
updatedAt: new Date("2024-02-01T11:37:00.130Z"),
};
mock.method(queries, "getApiKeyByUserId").mock.mockImplementation(
async () => apikey
);

const response = await getApikey(req, res, () => {
return 1;
});
assert.deepStrictEqual(response, apikey);
});

test("Get API throws an error", async (t) => {
const req = {
params: {
keyId: "M8purwS5KWwdYMHKD7XBz",
},
user: {
id: new mongoose.Types.ObjectId("652e67318ff18780e2eb5bf5"),
},
};
const res = {
status: (code: number) => ({
json: (data: any) => ({ data, code }),
}),
};
mock.method(queries, "getApiKeyByUserId").mock.mockImplementation(
async () => {
throw new Error("Error in get api key");
}
);

const response = await getApikey(req, res, () => {
return 1;
});
assert.strictEqual(response.data.error, "Error in get api key");
assert.strictEqual(response.code, 500);
});

test("Delete API succeeds", async (t) => {
const req = {
params: {
keyId: "abc123",
},
user: {
id: "123",
},
};

const res = {
status: () => ({
json: (data: any) => data,
}),
};

mock.method(queries, "deleteApiKey").mock.mockImplementation(
async () => ({ message: SUCCESS })
);
const response = await deleteApikey(req, res, () => {
return 1;
});
assert.strictEqual(response.message, SUCCESS);
});

test("Delete API throws an error", async (t) => {
const req = {
params: {
keyId: "abc123",
},
user: {
id: "123",
},
};

const res = {
status: (code: number) => ({
json: (data: any) => ({ data, code }),
}),
};

mock.method(queries, "deleteApiKey").mock.mockImplementation(
async () => {
throw new Error("Error in deleting");
}
);
const response: any = await deleteApikey(req, res, () => {
return 1;
});
assert.strictEqual(response.data.error, "Error in deleting");
assert.strictEqual(response.code, 500);
});
});
84 changes: 84 additions & 0 deletions apps/api/__tests__/apikey/middleware.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import test, { afterEach, describe, mock } from "node:test";
import assert from "node:assert";
import apikey from "../../src/apikey/middleware";
import queries from "../../src/apikey/queries";
import { BAD_REQUEST, UNAUTHORISED } from "../../src/config/strings";

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

test("Api key does not exist", async (t) => {
const req = {
body: {},
};

const res = {
status: (code: number) => ({
json: (data: any) => ({ data, code }),
}),
};

const response = await apikey(req, res, () => {
return 1;
});

assert.strictEqual(response.data.error, BAD_REQUEST);
assert.strictEqual(response.code, 400);
});

test("Unauthorised if Api key not found", async (t) => {
const req = {
body: {
apikey: "test",
},
};

const res = {
status: (code: number) => ({
json: (data: any) => ({ data, code }),
}),
};

mock.method(queries, "getApiKeyUsingKeyId").mock.mockImplementation(
async () => null
);

const response = await apikey(req, res, () => {
return 1;
});

assert.strictEqual(response.data.error, UNAUTHORISED);
assert.strictEqual(response.code, 401);
});

// test("InternalKey not exist", async (t) => {
// const req = {
// body: {
// apikey: "test",
// internalKey: "test",
// }
// };

// const res = {
// status: (code: number) => ({
// json: (data: any) => ({data, code}),
// })
// };

// mock.method(queries, "getApiKeyUsingKeyId").mock.mockImplementation(
// async () => ({key: "test key"})
// )

// mock.method(queries, "getApiKeyUsingKeyId").mock.mockImplementation(
// async () => (null)
// )

// const response = await apikey(req, res, () => {return 1})
// console.log("response internal", response);

// assert.strictEqual(response.data.error, UNAUTHORISED)
// assert.strictEqual(response.code, 401);
// })
});
65 changes: 65 additions & 0 deletions apps/api/__tests__/media-settings/handlers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import test, { afterEach, describe, mock } from "node:test";
import assert from "node:assert";
import { getMediaSettingsHandler } from "../../src/media-settings/handlers";
import mongoose from "mongoose";
import queries from "../../src/media-settings/queries";

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

test("Get media settings", async (t) => {
const req = {
user: {
id: new mongoose.Types.ObjectId("652e67318ff18780e2eb5bf5"),
},
apikey: "test",
};

const res = {
status: () => ({
json: (data: any) => data,
}),
};

mock.method(queries, "getMediaSettings").mock.mockImplementation(
async () => ({
useWebP: true,
webpOutputQuality: 85,
thumbnailHeight: 1,
thumbnailWidth: 1,
})
);

const response = await getMediaSettingsHandler(req, res);
assert.deepStrictEqual(response, {
useWebP: true,
webpOutputQuality: 85,
thumbnailHeight: 1,
thumbnailWidth: 1,
});
});

test("Get media settings return empty {} if mediasettings not found", async (t) => {
const req = {
user: {
id: new mongoose.Types.ObjectId("652e67318ff18780e2eb5bf5"),
},
apikey: "test",
};

const res = {
status: () => ({
json: (data: any) => data,
}),
};

mock.method(queries, "getMediaSettings").mock.mockImplementation(
async () => null
);

const response = await getMediaSettingsHandler(req, res);
assert.deepStrictEqual(response, {});
});
});
Loading
Loading