Kyle Gammon
03/07/2018, 11:53 AMmax
03/07/2018, 12:37 PMimport { GraphQLClient } from "graphql-request";
const { GRAPHQL_SERVER_URL, WRONG_TOKEN, TEST_EMAIL, TEST_PASSWORD, TEST_NAME } = process.env
interface User {
id: string
name: string
email: string
}
const userQuery = `
{
user {
id
name
email
}
}
`;
const authenticationMutation = `
mutation authenticateUser($email: String! $password: String!) {
authenticateUser(email: $email, password: $password) {
token
user {
id
name
email
}
}
}
`;
test("if token is malformed, it should throw an error", () => {
expect.assertions(1);
const config = {
headers: {
Authorization: "123"
}
}
const client = new GraphQLClient(GRAPHQL_SERVER_URL, config);
return client.request(userQuery)
.then(response =>
// expect(response).toEqual({ user: null })
console.log('response',response)
)
.catch(e => {
// console.log('e.response',e.response)
expect(e.response.errors[0].message).toEqual("jwt malformed")
});
});
test("if token is invalid, it should throw an error", () => {
expect.assertions(1);
const config = {
headers: {
Authorization: `Bearer ${WRONG_TOKEN}`
}
}
const client = new GraphQLClient(GRAPHQL_SERVER_URL, config);
return client.request(userQuery)
.then(response =>
// expect(response).toEqual({ user: null })
console.log('response',response)
)
.catch(e => {
// console.log('e.response',e.response)
expect(e.response.errors[0].message).toEqual("invalid signature")
});
});
max
03/07/2018, 12:54 PMVictorious
03/07/2018, 1:38 PMKyle Gammon
03/07/2018, 2:08 PMmax
03/07/2018, 2:16 PMAuthorizations
. See the config
var below:
test("if token is valid, it should return user details", () => {
expect.assertions(2);
const config = {
headers: {
Authorization: `Bearer ${USER_TOKEN}`
}
}
const client = new GraphQLClient(GRAPHQL_SERVER_URL, config);
return client.request(userQuery)
.then( (response: {user: User}) => {
// console.log('response',response)
expect(response.user.name).toEqual(TEST_NAME)
expect(response.user.email).toEqual(TEST_EMAIL)
})
.catch(e => console.log('e',e) );
});
Victorious
03/07/2018, 3:50 PM