Skip to content

Commit aa6ac81

Browse files
author
DylanBulmer
committed
fix ACL; fix dep issue; update tests and build script
1 parent 983b86e commit aa6ac81

File tree

7 files changed

+36
-14
lines changed

7 files changed

+36
-14
lines changed

bin/post-build.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
cp package.json README.md lib/
2+
rm -rf ./lib/types/__tests__
3+
rm -rf ./lib/esm/__tests__
4+
15
cat >lib/cjs/package.json <<!EOF
26
{
37
"type": "commonjs"

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@codrjs/core",
3-
"version": "1.0.6-patch7",
3+
"version": "1.0.7",
44
"description": "An open-sourced customizable annotation tool",
55
"main": "./cjs/index.js",
66
"module": "./esm/index.js",
@@ -21,7 +21,7 @@
2121
"test": "jest --config jest.config.json --passWithNoTests --coverage",
2222
"build:esm": "tsc --project tsconfig.esm.json",
2323
"build:cjs": "tsc --project tsconfig.cjs.json",
24-
"build": "yarn clean && yarn build:cjs && yarn build:esm && cp package.json README.md lib/ && rm -rf ./lib/types/__tests__ && ./bin/post-build.sh",
24+
"build": "yarn clean && yarn build:cjs && yarn build:esm && ./bin/post-build.sh",
2525
"clean": "rm -rf ./lib",
2626
"format": "prettier --write \"src/**/*.(ts|js)\"",
2727
"lint": "eslint -c .eslintrc.json --ignore-path .eslintignore --ext .ts src",

src/__tests__/AccessToken.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ describe("Access Token and Access Code generation", () => {
4141
expect(token3.toJSON().uuid).toBe(uuid3);
4242
});
4343

44+
it("properly validates a token", () => {
45+
expect(token2.isValid(uuid2 as unknown as typeof uuidv4)).toBe(true);
46+
});
47+
48+
it("properly expires a token", () => {
49+
token2.use();
50+
expect(token2.isValid(uuid2 as unknown as typeof uuidv4)).toBe(false);
51+
expect(token2.toJSON().expired).toBe(true);
52+
});
53+
4454
it("fails to generate a token", () => {
4555
const test1 = () => {
4656
// eslint-disable-next-line @typescript-eslint/ban-ts-comment

src/models/Profile.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { AccessibleRecordModel } from "@casl/mongoose";
1+
import {
2+
accessibleFieldsPlugin,
3+
AccessibleModel,
4+
accessibleRecordsPlugin,
5+
} from "@casl/mongoose";
26
import { Schema, model, Document } from "mongoose";
37

48
export interface IProfile extends Document {
@@ -8,7 +12,7 @@ export interface IProfile extends Document {
812

913
const ProfileSchema = new Schema<IProfile>(
1014
{
11-
username: { type: String, required: true },
15+
username: { type: String, required: true, unique: true },
1216
userId: {
1317
type: Schema.Types.ObjectId,
1418
ref: "User",
@@ -21,7 +25,9 @@ const ProfileSchema = new Schema<IProfile>(
2125
);
2226

2327
// exports User model.
24-
const Profile = model<IProfile, AccessibleRecordModel<IProfile>>(
28+
ProfileSchema.plugin(accessibleFieldsPlugin);
29+
ProfileSchema.plugin(accessibleRecordsPlugin);
30+
const Profile = model<IProfile, AccessibleModel<IProfile>>(
2531
"Profile",
2632
ProfileSchema,
2733
);

src/models/User.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { EmailRegex } from "../classes/Email.js";
2+
import {
3+
accessibleFieldsPlugin,
4+
AccessibleModel,
5+
accessibleRecordsPlugin,
6+
} from "@casl/mongoose";
27
import { Schema, model, Document } from "mongoose";
3-
import { AccessibleRecordModel } from "@casl/mongoose";
48

59
interface IUserProvider {
610
photo?: string;
@@ -77,5 +81,7 @@ UserSchema.virtual("fullName").get(function get() {
7781
});
7882

7983
// exports User model.
80-
const User = model<IUser, AccessibleRecordModel<IUser>>("User", UserSchema);
84+
UserSchema.plugin(accessibleFieldsPlugin);
85+
UserSchema.plugin(accessibleRecordsPlugin);
86+
const User = model<IUser, AccessibleModel<IUser>>("User", UserSchema);
8187
export default User;

src/services/app.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
* it access to necessary configuration to access the database, authenticating
44
* with the server, and other uses.
55
*/
6-
7-
import { accessibleRecordsPlugin } from "@casl/mongoose";
86
import User from "../models/User.js";
97
import mongoose, { Mongoose } from "mongoose";
108

@@ -59,7 +57,6 @@ class App implements AppOptions {
5957
try {
6058
// connect to the database.
6159
this.mongo = await mongoose.connect(this.databaseUri);
62-
this.mongo.plugin(accessibleRecordsPlugin);
6360

6461
// try to create an admin user.
6562
try {

src/services/database.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
* This service handles all configuration handlers for codr.
33
*/
44

5-
import { UserToken } from "../classes/JWT.js";
65
import Profile, { IProfile } from "../models/Profile.js";
76
import ProfileAbility from "../models/Profile.ability.js";
87
import User, { IUser } from "../models/User.js";
98
import UserAbility from "../models/User.ability.js";
109
import App from "./app.js";
1110
import { QueryWithHelpers, Types } from "mongoose";
12-
import type { AccessibleRecordQueryHelpers } from "../../node_modules/@casl/mongoose/dist/types/accessible_records.js";
11+
import type { AccessibleRecordQueryHelpers } from "@casl/mongoose/dist/types/accessible_records.js";
1312

1413
class Database {
1514
private app: App;
@@ -18,7 +17,7 @@ class Database {
1817
}
1918

2019
User(
21-
token: UserToken,
20+
token: IUser,
2221
): QueryWithHelpers<
2322
IUser[],
2423
IUser,
@@ -33,7 +32,7 @@ class Database {
3332
}
3433
}
3534

36-
Profile(token: UserToken): QueryWithHelpers<
35+
Profile(token: IUser): QueryWithHelpers<
3736
(IProfile & {
3837
_id: Types.ObjectId;
3938
})[],

0 commit comments

Comments
 (0)