You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
153 lines
3.6 KiB
153 lines
3.6 KiB
5 years ago
|
import { gql } from "apollo-server"
|
||
|
import { createConnection, getConnection } from "typeorm"
|
||
5 years ago
|
import { callSchema, connectionOptions } from "./schema"
|
||
|
import { signToken, verifyToken } from "./userResolver/auth"
|
||
|
import { Tokens } from "./userResolver/Tokens"
|
||
|
import { User } from "./userResolver/User"
|
||
5 years ago
|
|
||
|
beforeAll(async () => {
|
||
5 years ago
|
return await createConnection(connectionOptions)
|
||
5 years ago
|
})
|
||
|
|
||
|
afterAll(async () => {
|
||
|
return await getConnection().close()
|
||
|
})
|
||
|
|
||
|
afterEach(async () => {
|
||
|
return await getConnection().synchronize(true)
|
||
|
})
|
||
|
|
||
|
describe("resolver of user", () => {
|
||
|
describe("createUser mutation should", () => {
|
||
|
it("return email as it creates user with mutation", async () => {
|
||
|
const createUserMutation = gql`
|
||
|
mutation {
|
||
|
createUser(email: "email@email.com", password: "password") {
|
||
|
email
|
||
|
}
|
||
|
}
|
||
|
`
|
||
|
|
||
|
const response = await callSchema(createUserMutation)
|
||
|
|
||
|
expect(response.errors).toBeUndefined()
|
||
|
expect(response.data).toMatchObject({
|
||
|
createUser: { email: "email@email.com" },
|
||
|
})
|
||
|
})
|
||
|
})
|
||
|
|
||
|
describe("users query should", () => {
|
||
|
it("return emails of registered users", async () => {
|
||
|
const usersQuery = gql`
|
||
|
query {
|
||
|
users {
|
||
|
email
|
||
|
}
|
||
|
}
|
||
|
`
|
||
|
|
||
|
const user = await User.create({
|
||
|
email: "email@email.com",
|
||
|
}).save()
|
||
|
|
||
|
const response = await callSchema(usersQuery)
|
||
|
|
||
|
expect(response.errors).toBeUndefined()
|
||
|
expect(response.data).toMatchObject({
|
||
|
users: [{ email: user.email }],
|
||
|
})
|
||
|
})
|
||
|
})
|
||
|
|
||
5 years ago
|
describe("tokens query should", () => {
|
||
|
const tokensQuery = gql`
|
||
5 years ago
|
query {
|
||
5 years ago
|
tokens(email: "email@email.com", password: "good-password") {
|
||
5 years ago
|
accessToken
|
||
|
}
|
||
|
}
|
||
|
`
|
||
5 years ago
|
|
||
5 years ago
|
it("return error for non-existent user", async () => {
|
||
5 years ago
|
const response = await callSchema(tokensQuery)
|
||
5 years ago
|
|
||
|
expect(response.errors).not.toBeUndefined()
|
||
|
expect(response.data).toBeNull()
|
||
|
})
|
||
|
|
||
|
it("return error for bad password", async () => {
|
||
|
await User.create({
|
||
|
email: "email@email.com",
|
||
|
password: "BAD-password",
|
||
|
}).save()
|
||
|
|
||
5 years ago
|
const response = await callSchema(tokensQuery)
|
||
5 years ago
|
|
||
|
expect(response.errors).not.toBeUndefined()
|
||
|
expect(response.data).toBeNull()
|
||
|
})
|
||
|
|
||
|
it("return a valid access token with good credentials", async () => {
|
||
|
await User.create({
|
||
|
email: "email@email.com",
|
||
|
password: "good-password",
|
||
|
}).save()
|
||
|
|
||
5 years ago
|
const response = await callSchema(tokensQuery)
|
||
|
const token = response.data!.tokens.accessToken
|
||
|
const tokens = new Tokens()
|
||
|
tokens.accessToken = token
|
||
5 years ago
|
|
||
5 years ago
|
expect(response.errors).toBeUndefined()
|
||
|
expect(response.data).toMatchObject({ tokens })
|
||
5 years ago
|
expect(verifyToken(token)).toBeTruthy()
|
||
|
})
|
||
|
})
|
||
|
|
||
|
describe("me query should", () => {
|
||
|
const meQuery = gql`
|
||
|
query {
|
||
|
me {
|
||
|
email
|
||
|
}
|
||
|
}
|
||
|
`
|
||
|
|
||
|
it("return an error without a valid jwt token", async () => {
|
||
5 years ago
|
const context = {
|
||
|
req: {
|
||
|
headers: {
|
||
|
authorization: "Bearer INVALID-TOKEN",
|
||
|
},
|
||
|
},
|
||
|
}
|
||
|
const response = await callSchema(meQuery, context)
|
||
5 years ago
|
|
||
|
expect(response.errors).not.toBeUndefined()
|
||
|
expect(response.data).toBeNull()
|
||
|
})
|
||
|
|
||
|
it("return an user with a valid jwt token", async () => {
|
||
|
const user = await User.create({
|
||
|
email: "email@email.com",
|
||
|
}).save()
|
||
|
|
||
|
const context = {
|
||
|
req: {
|
||
|
headers: {
|
||
|
authorization: "Bearer " + signToken({ userId: user.id }),
|
||
|
},
|
||
|
},
|
||
|
}
|
||
|
|
||
|
const response = await callSchema(meQuery, context)
|
||
|
|
||
|
expect(response.errors).toBeUndefined()
|
||
|
expect(response.data).toMatchObject({
|
||
|
me: { email: user.email },
|
||
|
})
|
||
|
})
|
||
|
})
|
||
|
})
|