-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
212 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
208 changes: 208 additions & 0 deletions
208
tests/directives/directiveTransformer/newAuthDirectiveTransformer.spec.ts
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,208 @@ | ||
import { beforeAll, afterAll, it, expect } from "vitest"; | ||
import { connect, disconnect } from "../../helpers/db"; | ||
import type mongoose from "mongoose"; | ||
import { ApolloServer } from "@apollo/server"; | ||
import { gql } from "graphql-tag"; | ||
import "dotenv/config"; | ||
import i18n from "i18n"; | ||
import express from "express"; | ||
import type { TestUserType } from "../../helpers/userAndOrg"; | ||
import { createTestUserFunc } from "../../helpers/user"; | ||
import { makeExecutableSchema } from "@graphql-tools/schema"; | ||
import authDirectiveTransformer from "../../../src/directives/directiveTransformer/authDirectiveTransformer"; | ||
import roleDirectiveTransformer from "../../../src/directives/directiveTransformer/roleDirectiveTransformer"; | ||
import { appConfig } from "../../../src/config"; | ||
import { errors } from "../../../src/libraries"; | ||
import newauthDirectiveTransformer from "../../../src/directives/directiveTransformer/newAuthTransformer"; | ||
|
||
let testUser: TestUserType; | ||
|
||
const typeDefs = gql` | ||
directive @newauth on FIELD_DEFINITION | ||
type UnauthenticatedError { | ||
message: String | ||
path: [String] | ||
} | ||
type QueryResult { | ||
data: String | ||
errors: [UnauthenticatedError] | ||
} | ||
type Query { | ||
hello: QueryResult! @newauth | ||
} | ||
`; | ||
|
||
const resolvers = { | ||
Query: { | ||
//@ts-ignore | ||
hello: (_parent, args, context) => { | ||
const errors = context.errors || []; | ||
return { | ||
data: "hi", | ||
errors: errors, | ||
}; | ||
}, | ||
}, | ||
}; | ||
|
||
let MONGOOSE_INSTANCE: typeof mongoose; | ||
|
||
beforeAll(async () => { | ||
MONGOOSE_INSTANCE = await connect(); | ||
testUser = await createTestUserFunc(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await testUser!.remove(); | ||
await disconnect(MONGOOSE_INSTANCE); | ||
}); | ||
|
||
it("adds UnauthenticatedError when context is expired", async () => { | ||
const query = ` | ||
query { | ||
hello { | ||
data | ||
errors { | ||
__typename | ||
message | ||
} | ||
} | ||
} | ||
`; | ||
const authenticatedContext = { | ||
expired: true, | ||
}; | ||
let schema = makeExecutableSchema({ | ||
typeDefs, | ||
resolvers, | ||
}); | ||
// defines directives | ||
schema = authDirectiveTransformer(schema, "auth"); | ||
schema = newauthDirectiveTransformer(schema, "newauth"); | ||
schema = roleDirectiveTransformer(schema, "role"); | ||
const apolloServer = new ApolloServer({ | ||
schema, | ||
}); | ||
const result = await apolloServer.executeOperation( | ||
{ | ||
query, | ||
variables: {}, | ||
}, | ||
{ | ||
contextValue: authenticatedContext, | ||
} | ||
); | ||
|
||
//@ts-ignore | ||
expect(result.body.singleResult.data).toEqual({ | ||
hello: { | ||
data: "hi", | ||
errors: [ | ||
{ | ||
__typename: "UnauthenticatedError", | ||
message: "You are not authenticated", | ||
}, | ||
], | ||
}, | ||
}); | ||
}); | ||
|
||
it("adds UnauthenticatedError when context: isAuth == false", async () => { | ||
const query = ` | ||
query { | ||
hello { | ||
data | ||
errors { | ||
__typename | ||
message | ||
} | ||
} | ||
} | ||
`; | ||
const authenticatedContext = { | ||
expired: true, | ||
isAuth: false, | ||
}; | ||
let schema = makeExecutableSchema({ | ||
typeDefs, | ||
resolvers, | ||
}); | ||
// defines directives | ||
schema = authDirectiveTransformer(schema, "auth"); | ||
schema = newauthDirectiveTransformer(schema, "newauth"); | ||
schema = roleDirectiveTransformer(schema, "role"); | ||
const apolloServer = new ApolloServer({ | ||
schema, | ||
}); | ||
const result = await apolloServer.executeOperation( | ||
{ | ||
query, | ||
variables: {}, | ||
}, | ||
{ | ||
contextValue: authenticatedContext, | ||
} | ||
); | ||
|
||
//@ts-ignore | ||
expect(result.body.singleResult.data).toEqual({ | ||
hello: { | ||
data: "hi", | ||
errors: [ | ||
{ | ||
__typename: "UnauthenticatedError", | ||
message: "You are not authenticated", | ||
}, | ||
], | ||
}, | ||
}); | ||
}); | ||
|
||
it("returns data if isAuth == true and expire == false", async () => { | ||
const query = ` | ||
query { | ||
hello { | ||
data | ||
errors { | ||
__typename | ||
message | ||
} | ||
} | ||
} | ||
`; | ||
const authenticatedContext = { | ||
expired: false, | ||
isAuth: true, | ||
}; | ||
let schema = makeExecutableSchema({ | ||
typeDefs, | ||
resolvers, | ||
}); | ||
// defines directives | ||
schema = authDirectiveTransformer(schema, "auth"); | ||
schema = newauthDirectiveTransformer(schema, "newauth"); | ||
schema = roleDirectiveTransformer(schema, "role"); | ||
const apolloServer = new ApolloServer({ | ||
schema, | ||
}); | ||
const result = await apolloServer.executeOperation( | ||
{ | ||
query, | ||
variables: {}, | ||
}, | ||
{ | ||
contextValue: authenticatedContext, | ||
} | ||
); | ||
|
||
//@ts-ignore | ||
expect(result.body.singleResult.data).toEqual({ | ||
hello: { | ||
data: "hi", | ||
errors: [], | ||
}, | ||
}); | ||
}); |