-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Use native DB type in injected auth models if user uses it #2243
Changes from 4 commits
788202b
9e93eeb
048d804
a197218
362ccff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A lot of AST here which is a bit hard to read / maintain -> any way we might be able to replace it with actual PSL, here in the test? That is parsed either during test runtime, or even test compile time if that is doable (although I think I remember we had hard time with that). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I've done this bit since it was similar to something we already did with the full schema file. wasp/waspc/test/Util/Prisma.hs Line 10 in 362ccff
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,239 @@ | ||
module Generator.AuthInjectionTest where | ||
|
||
import Data.Maybe (maybeToList) | ||
import Test.Tasty.Hspec | ||
import qualified Wasp.AppSpec.Entity as AS.Entity | ||
import Wasp.Generator.DbGenerator.Auth (injectAuth) | ||
import Wasp.Generator.Monad (runGenerator) | ||
import qualified Wasp.Psl.Ast.Argument as Psl.Argument | ||
import qualified Wasp.Psl.Ast.Attribute as Psl.Attribute | ||
import qualified Wasp.Psl.Ast.Model as Psl.Model | ||
|
||
data UserEntityIdField = UserEntityIdField | ||
{ _type :: Psl.Model.FieldType, | ||
_nativeDbType :: Maybe Psl.Attribute.Attribute | ||
} | ||
|
||
spec_GeneratorAuthInjectionTest :: Spec | ||
spec_GeneratorAuthInjectionTest = do | ||
describe "injectAuth" $ do | ||
it "injects auth entities and user entity relation" $ do | ||
testAuthInjection $ | ||
UserEntityIdField | ||
{ _type = Psl.Model.Int, | ||
_nativeDbType = Nothing | ||
} | ||
|
||
it "injects auth entities and user entity relation (user ID is a native db field)" $ do | ||
testAuthInjection $ | ||
UserEntityIdField | ||
{ _type = Psl.Model.String, | ||
_nativeDbType = Just $ Psl.Attribute.Attribute "db.Uuid" [] | ||
} | ||
where | ||
testAuthInjection :: UserEntityIdField -> Expectation | ||
testAuthInjection | ||
UserEntityIdField | ||
{ _type = userEntityIdFieldType, | ||
_nativeDbType = maybeUserEntityIdFieldNativeDbType | ||
} = do | ||
let userEntityIdField = | ||
Psl.Model.ElementField $ | ||
Psl.Model.Field | ||
"id" | ||
userEntityIdFieldType | ||
[] | ||
(Psl.Attribute.Attribute "id" [] : maybeToList maybeUserEntityIdFieldNativeDbType) | ||
let userEntity = | ||
( "User", | ||
AS.Entity.makeEntity $ | ||
Psl.Model.Body [userEntityIdField] | ||
) | ||
let authEntityRelation = | ||
Psl.Model.ElementField $ | ||
Psl.Model.Field | ||
"auth" | ||
(Psl.Model.UserType "Auth") | ||
[Psl.Model.Optional] | ||
[] | ||
let userEntityWithInjectedRelationship = | ||
( "User", | ||
AS.Entity.makeEntity $ | ||
Psl.Model.Body [userEntityIdField, authEntityRelation] | ||
) | ||
let authEntity = makeAuthEntity userEntityIdFieldType maybeUserEntityIdFieldNativeDbType | ||
|
||
let allEntities = [userEntity, someOtherEntity] | ||
let (_generatorWarnings, generatorResult) = runGenerator $ injectAuth allEntities userEntity | ||
in generatorResult | ||
`shouldBe` Right | ||
[ userEntityWithInjectedRelationship, | ||
someOtherEntity, | ||
authEntity, | ||
authIdentityEntity, | ||
sessionEntity | ||
] | ||
|
||
makeAuthEntity :: Psl.Model.FieldType -> Maybe Psl.Attribute.Attribute -> (String, AS.Entity.Entity) | ||
makeAuthEntity userEntityIdFieldType maybeUserEntityIdFieldNativeDbType = | ||
let userIdField = makeAuthEntityUserIdField userEntityIdFieldType maybeUserEntityIdFieldNativeDbType | ||
in ( "Auth", | ||
AS.Entity.makeEntity | ||
( Psl.Model.Body | ||
[ Psl.Model.ElementField $ | ||
Psl.Model.Field | ||
"id" | ||
Psl.Model.String | ||
[] | ||
[ Psl.Attribute.Attribute "id" [], | ||
Psl.Attribute.Attribute "default" [Psl.Argument.ArgUnnamed $ Psl.Argument.FuncExpr "uuid" []] | ||
], | ||
userIdField, | ||
Psl.Model.ElementField $ | ||
Psl.Model.Field | ||
"user" | ||
(Psl.Model.UserType "User") | ||
[Psl.Model.Optional] | ||
[ Psl.Attribute.Attribute | ||
"relation" | ||
[ Psl.Argument.ArgNamed "fields" (Psl.Argument.ArrayExpr [Psl.Argument.IdentifierExpr "userId"]), | ||
Psl.Argument.ArgNamed "references" (Psl.Argument.ArrayExpr [Psl.Argument.IdentifierExpr "id"]), | ||
Psl.Argument.ArgNamed "onDelete" (Psl.Argument.IdentifierExpr "Cascade") | ||
] | ||
], | ||
Psl.Model.ElementField $ | ||
Psl.Model.Field | ||
"identities" | ||
(Psl.Model.UserType "AuthIdentity") | ||
[Psl.Model.List] | ||
[], | ||
Psl.Model.ElementField $ | ||
Psl.Model.Field | ||
"sessions" | ||
(Psl.Model.UserType "Session") | ||
[Psl.Model.List] | ||
[] | ||
] | ||
) | ||
) | ||
|
||
makeAuthEntityUserIdField :: Psl.Model.FieldType -> Maybe Psl.Attribute.Attribute -> Psl.Model.Element | ||
makeAuthEntityUserIdField userEntityIdFieldType maybeUserEntityIdFieldNativeDbType = | ||
let userIdFieldAttributes = (Psl.Attribute.Attribute "unique" [] : maybeToList maybeUserEntityIdFieldNativeDbType) | ||
in Psl.Model.ElementField $ | ||
Psl.Model.Field | ||
"userId" | ||
userEntityIdFieldType | ||
[Psl.Model.Optional] | ||
userIdFieldAttributes | ||
|
||
authIdentityEntity = | ||
( "AuthIdentity", | ||
AS.Entity.makeEntity | ||
( Psl.Model.Body | ||
[ Psl.Model.ElementField $ | ||
Psl.Model.Field | ||
"providerName" | ||
Psl.Model.String | ||
[] | ||
[], | ||
Psl.Model.ElementField $ | ||
Psl.Model.Field | ||
"providerUserId" | ||
Psl.Model.String | ||
[] | ||
[], | ||
Psl.Model.ElementField $ | ||
Psl.Model.Field | ||
"providerData" | ||
Psl.Model.String | ||
[] | ||
[ Psl.Attribute.Attribute "default" [Psl.Argument.ArgUnnamed $ Psl.Argument.StringExpr "{}"] | ||
], | ||
Psl.Model.ElementField $ | ||
Psl.Model.Field | ||
"authId" | ||
Psl.Model.String | ||
[] | ||
[], | ||
Psl.Model.ElementField $ | ||
Psl.Model.Field | ||
"auth" | ||
(Psl.Model.UserType "Auth") | ||
[] | ||
[ Psl.Attribute.Attribute | ||
"relation" | ||
[ Psl.Argument.ArgNamed "fields" (Psl.Argument.ArrayExpr [Psl.Argument.IdentifierExpr "authId"]), | ||
Psl.Argument.ArgNamed "references" (Psl.Argument.ArrayExpr [Psl.Argument.IdentifierExpr "id"]), | ||
Psl.Argument.ArgNamed "onDelete" (Psl.Argument.IdentifierExpr "Cascade") | ||
] | ||
], | ||
Psl.Model.ElementBlockAttribute $ | ||
Psl.Attribute.Attribute "id" [Psl.Argument.ArgUnnamed $ Psl.Argument.ArrayExpr [Psl.Argument.IdentifierExpr "providerName", Psl.Argument.IdentifierExpr "providerUserId"]] | ||
] | ||
) | ||
) | ||
|
||
sessionEntity = | ||
( "Session", | ||
AS.Entity.makeEntity | ||
( Psl.Model.Body | ||
[ Psl.Model.ElementField $ | ||
Psl.Model.Field | ||
"id" | ||
Psl.Model.String | ||
[] | ||
[ Psl.Attribute.Attribute "id" [], | ||
Psl.Attribute.Attribute "unique" [] | ||
], | ||
Psl.Model.ElementField $ | ||
Psl.Model.Field | ||
"expiresAt" | ||
Psl.Model.DateTime | ||
[] | ||
[], | ||
Psl.Model.ElementField $ | ||
Psl.Model.Field | ||
"userId" | ||
Psl.Model.String | ||
[] | ||
[], | ||
Psl.Model.ElementField $ | ||
Psl.Model.Field | ||
"auth" | ||
(Psl.Model.UserType "Auth") | ||
[] | ||
[ Psl.Attribute.Attribute | ||
"relation" | ||
[ Psl.Argument.ArgNamed "references" (Psl.Argument.ArrayExpr [Psl.Argument.IdentifierExpr "id"]), | ||
Psl.Argument.ArgNamed "fields" (Psl.Argument.ArrayExpr [Psl.Argument.IdentifierExpr "userId"]), | ||
Psl.Argument.ArgNamed "onDelete" (Psl.Argument.IdentifierExpr "Cascade") | ||
] | ||
], | ||
Psl.Model.ElementBlockAttribute $ | ||
Psl.Attribute.Attribute | ||
"index" | ||
[ Psl.Argument.ArgUnnamed $ Psl.Argument.ArrayExpr [Psl.Argument.IdentifierExpr "userId"] | ||
] | ||
] | ||
) | ||
) | ||
|
||
someOtherEntity = | ||
( "SomeOtherEntity", | ||
AS.Entity.makeEntity | ||
( Psl.Model.Body | ||
[ Psl.Model.ElementField $ | ||
Psl.Model.Field | ||
"id" | ||
Psl.Model.Int | ||
[] | ||
[ Psl.Attribute.Attribute "id" [], | ||
Psl.Attribute.Attribute | ||
"default" | ||
[ Psl.Argument.ArgUnnamed $ Psl.Argument.FuncExpr "autoincrement" [] | ||
] | ||
] | ||
] | ||
) | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reads nicer :D