Skip to content

Commit

Permalink
test(delegate): add @password interaction test (#1721)
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 authored Sep 20, 2024
1 parent 57652a1 commit cb68815
Showing 1 changed file with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { loadSchema } from '@zenstackhq/testtools';
import { compareSync } from 'bcryptjs';

describe('Polymorphic @omit', () => {
const model = `
model User {
id Int @id @default(autoincrement())
posts Post[]
@@allow('all', true)
}
model Asset {
id Int @id @default(autoincrement())
type String
assetPassword String @password
@@delegate(type)
@@allow('all', true)
}
model Post extends Asset {
title String
postPassword String @password
user User? @relation(fields: [userId], references: [id])
userId Int? @unique
}
`;

it('hashes when created directly', async () => {
const { enhance } = await loadSchema(model);

const db = enhance();
const post = await db.post.create({ data: { title: 'Post1', assetPassword: 'asset', postPassword: 'post' } });
expect(compareSync('asset', post.assetPassword)).toBeTruthy();
expect(compareSync('post', post.postPassword)).toBeTruthy();
});

it('hashes when created nested', async () => {
const { enhance } = await loadSchema(model);

const db = enhance();
const user = await db.user.create({
data: { posts: { create: { title: 'Post1', assetPassword: 'asset', postPassword: 'post' } } },
include: { posts: true },
});
expect(compareSync('asset', user.posts[0].assetPassword)).toBeTruthy();
expect(compareSync('post', user.posts[0].postPassword)).toBeTruthy();
});
});

0 comments on commit cb68815

Please sign in to comment.