This repository has been archived by the owner on May 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: add some storybook https://redwoodjs.com/docs/tutorial/chapter5/first-story * fix: enable show code button storybookjs/storybook#8104 (comment) * fix: better layout * chore: make boilerplate stories TODO have framework generate these * fix: account for missing id on netlify > Error: Variable "$id" of required type "Int!" was not provided. * chore: fixup some tests https://redwoodjs.com/docs/tutorial/chapter5/first-test * chore: add `Comment` yarn rw g component Comment https://redwoodjs.com/docs/tutorial/chapter6/the-redwood-way * chore: add some boilerplate * chore(Comment): improve and wire up some mock data https://redwoodjs.com/docs/tutorial/chapter6/the-redwood-way#storybook * chore: add styling * chore: add CommentsCell https://redwoodjs.com/docs/tutorial/chapter6/multiple-comments * chore: add some boilerplate * chore: add better mocks * chore: update CommentsCell * chore: add a bit more styling * chore: add comments TODO figure out loading part of this * chore: fix * chore: add Comments to the schema https://redwoodjs.com/docs/tutorial/chapter6/comments-schema * chore(prisma): generate Comment table migration * chore(prisma): generate Comment table migration * chore(ide): add recommended extension * chore: remove unused routes in main app * Revert "chore: remove unused routes in main app" This reverts commit 53637f8. * chore: disable contact/login forms not used * chore: remove unused import * test(unit): add example async test * test(unit): default summary to true * chore: update empty comments cell https://redwoodjs.com/docs/tutorial/chapter6/comments-schema * chore(comments): allow/create https://redwoodjs.com/docs/tutorial/chapter6/comments-schema * chore(commentForm): run generator `yarn rw g component CommentForm` https://redwoodjs.com/docs/tutorial/chapter6/comment-form * chore(commentForm): add some boilerplate * chore(commentForm): simple form * chore(commentForm): add submit * chore(storybook): wire up mockGraphQLMutation * chore(storybook): add interaction test https://stackoverflow.com/a/63745654 * chore: fixup style * test(unit): better test * test: add loading snapshot * chore(commentForm): use form https://redwoodjs.com/docs/tutorial/chapter6/comment-form#adding-the-form-to-the-blog-post * chore(commentForm): wire up correctly in Article * chore(contactForm): refetch comments after create https://redwoodjs.com/docs/tutorial/chapter6/comment-form#graphql-query-caching * chore: add toast feedback for form https://redwoodjs.com/docs/tutorial/chapter6/comment-form#graphql-query-caching * chore: wire up comments to posts correctly * chore: fixup prod/dev routes * chore: get comment by post * chore(rbac): add roles to user https://redwoodjs.com/docs/tutorial/chapter7/rbac * chore(rbac): gate admin page * chore(rbac): add seed script * chore: add default route for /admin * chore(rbac): add delete button also wire up test/story https://redwoodjs.com/docs/tutorial/chapter7/rbac#mocking-currentuser-for-jest * chore: gate delete call * chore: allow admin to delete as well * chore(rbac): wire up delete gating * test(unit): simplify * fix(storybook): add explicit imports for Netlify * build(🧶): add reslutions for `@storybook/*` transitive dependencies of redwood
- Loading branch information
1 parent
4a1b79d
commit 149289e
Showing
55 changed files
with
2,484 additions
and
431 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
api/db/migrations/20220402112722_create_comment/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
-- CreateTable | ||
CREATE TABLE "Comment" ( | ||
"id" SERIAL NOT NULL, | ||
"name" TEXT NOT NULL, | ||
"body" TEXT NOT NULL, | ||
"postId" INTEGER NOT NULL, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
|
||
CONSTRAINT "Comment_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Comment" ADD CONSTRAINT "Comment_postId_fkey" FOREIGN KEY ("postId") REFERENCES "Post"("id") ON DELETE RESTRICT ON UPDATE CASCADE; |
2 changes: 2 additions & 0 deletions
2
api/db/migrations/20220402115226_add_roles_to_user/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- AlterTable | ||
ALTER TABLE "User" ADD COLUMN "roles" TEXT NOT NULL DEFAULT E'moderator'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
export const schema = gql` | ||
type Comment { | ||
id: Int! | ||
name: String! | ||
body: String! | ||
post: Post! | ||
postId: Int! | ||
createdAt: DateTime! | ||
} | ||
type Query { | ||
comments(postId: Int!): [Comment!]! @skipAuth | ||
} | ||
input CreateCommentInput { | ||
name: String! | ||
body: String! | ||
postId: Int! | ||
} | ||
input UpdateCommentInput { | ||
name: String | ||
body: String | ||
postId: Int | ||
} | ||
type Mutation { | ||
createComment(input: CreateCommentInput!): Comment! @skipAuth | ||
deleteComment(id: Int!): Comment! | ||
@requireAuth(roles: ["moderator", "admin"]) | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { db } from 'src/lib/db' | ||
import { requireAuth } from 'src/lib/auth' | ||
|
||
export const comments = ({ postId }) => { | ||
return db.comment.findMany({ where: { postId } }) | ||
} | ||
|
||
export const comment = ({ id }) => { | ||
return db.comment.findUnique({ | ||
where: { id }, | ||
}) | ||
} | ||
|
||
export const Comment = { | ||
post: (_obj, { root }) => | ||
db.comment.findUnique({ where: { id: root.id } }).post(), | ||
} | ||
|
||
export const createComment = ({ input }) => { | ||
return db.comment.create({ | ||
data: input, | ||
}) | ||
} | ||
|
||
export const deleteComment = ({ id }) => { | ||
requireAuth({ roles: ['moderator', 'admin'] }) | ||
return db.comment.delete({ | ||
where: { id }, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
export const standard = defineScenario({ | ||
comment: { | ||
jane: { | ||
data: { | ||
name: 'Jane Doe', | ||
body: 'I like trees', | ||
post: { | ||
create: { | ||
title: 'Redwood Leaves', | ||
body: 'The quick brown fox jumped over the lazy dog.', | ||
}, | ||
}, | ||
}, | ||
}, | ||
john: { | ||
data: { | ||
name: 'John Doe', | ||
body: 'Hug a tree today', | ||
post: { | ||
create: { | ||
title: 'Root Systems', | ||
body: 'The five boxing wizards jump quickly.', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
export const postOnly = defineScenario({ | ||
post: { | ||
bark: { | ||
data: { | ||
title: 'Bark', | ||
body: "A tree's bark is worse than its bite", | ||
}, | ||
}, | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { comments, createComment, deleteComment } from './comments' | ||
import { db } from 'src/lib/db' | ||
import { AuthenticationError, ForbiddenError } from '@redwoodjs/graphql-server' | ||
|
||
// Generated boilerplate tests do not account for all circumstances | ||
// and can fail without adjustments, e.g. Float and DateTime types. | ||
// Please refer to the RedwoodJS Testing Docs: | ||
// https://redwoodjs.com/docs/testing#testing-services | ||
// https://redwoodjs.com/docs/testing#jest-expect-type-considerations | ||
|
||
describe('comments', () => { | ||
scenario( | ||
'returns all comments for a single post from the database', | ||
async (scenario) => { | ||
const result = await comments({ postId: scenario.comment.jane.postId }) | ||
const post = await db.post.findUnique({ | ||
where: { id: scenario.comment.jane.postId }, | ||
include: { comments: true }, | ||
}) | ||
expect(result.length).toEqual(post.comments.length) | ||
} | ||
) | ||
|
||
scenario('postOnly', 'creates a new comment', async (scenario) => { | ||
const comment = await createComment({ | ||
input: { | ||
name: 'Billy Bob', | ||
body: 'What is your favorite tree bark?', | ||
postId: scenario.post.bark.id, | ||
}, | ||
}) | ||
|
||
expect(comment.name).toEqual('Billy Bob') | ||
expect(comment.body).toEqual('What is your favorite tree bark?') | ||
expect(comment.postId).toEqual(scenario.post.bark.id) | ||
expect(comment.createdAt).not.toEqual(null) | ||
}) | ||
scenario( | ||
'allows admins and moderators to delete a comment', | ||
async (scenario) => { | ||
mockCurrentUser({ roles: ['admin', 'moderator'] }) | ||
|
||
const comment = await deleteComment({ | ||
id: scenario.comment.jane.id, | ||
}) | ||
expect(comment.id).toEqual(scenario.comment.jane.id) | ||
|
||
const result = await comments({ postId: scenario.comment.jane.id }) | ||
expect(result.length).toEqual(0) | ||
} | ||
) | ||
|
||
scenario( | ||
'does not allow a non-moderator to delete a comment', | ||
async (scenario) => { | ||
mockCurrentUser({ roles: 'user' }) | ||
|
||
expect(() => | ||
deleteComment({ | ||
id: scenario.comment.jane.id, | ||
}) | ||
).toThrow(ForbiddenError) | ||
} | ||
) | ||
|
||
scenario( | ||
'does not allow a logged out user to delete a comment', | ||
async (scenario) => { | ||
mockCurrentUser(null) | ||
|
||
expect(() => | ||
deleteComment({ | ||
id: scenario.comment.jane.id, | ||
}) | ||
).toThrow(AuthenticationError) | ||
} | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/usr/bin/node | ||
|
||
import CryptoJS from 'crypto-js' | ||
import { db } from 'api/src/lib/db' | ||
|
||
const user = 'moderator@moderator.com' | ||
const password = 'password' | ||
const salt = CryptoJS.lib.WordArray.random(128 / 8).toString() | ||
const hashedPassword = CryptoJS.PBKDF2(password, salt, { | ||
keySize: 256 / 32, | ||
}).toString() | ||
// TODO add flag for admin | ||
db.user.create({ | ||
data: { email: user, hashedPassword, salt }, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.