Skip to content
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

How can I test my middlewares? #129

Open
ngshiheng opened this issue Jan 19, 2020 · 0 comments
Open

How can I test my middlewares? #129

ngshiheng opened this issue Jan 19, 2020 · 0 comments

Comments

@ngshiheng
Copy link

Can you show me an example of how can i test my middlewares?
I want to test that my queries can only be accessed if the correct jwt token is passed through the request Authorization header. I'm using graphql-yoga middleware. Here's an example of my graphql server:

const server = new GraphQLServer({
    typeDefs: './src/schemas/schema.graphql',
    resolvers,
    middlewares: [permissions],   // <-- here's where I pass in my middleware
    context: request => {
        return {
            ...request,
            prisma,
        };
    },
});

Here's what I intend to do, e.g.:

test('only authorized user can see this', async () => {
        const query = `
             query user($id: ID!){
                user(id: $id) {
                    id
                    email
                    name
                }
            }
        `;
        const args = {
            id: 'foo-bar-id',
        };
        const result = await tester.graphql(
            query,
            {},
            { prisma },
            args,
        );
         expect(result.errors[0].message).to.be.eq(
             'Error!! Unauthorized request',
         );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant