generated from kimyvgy/worker-apollo-server-template
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tests and Types (and improvements to context passing on tests helpers)
- Loading branch information
Showing
5 changed files
with
118 additions
and
6 deletions.
There are no files selected for viewing
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
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,23 @@ | ||
/* eslint-disable */ | ||
/* @ts-nocheck */ | ||
/* prettier-ignore */ | ||
/* This file is automatically generated using `npm run graphql:types` */ | ||
import type * as Types from '../../../generated/types'; | ||
|
||
import type { JsonObject } from "type-fest"; | ||
import gql from 'graphql-tag'; | ||
export type TokensMutationVariables = Types.Exact<{ | ||
input: Types.RetoolToken; | ||
}>; | ||
|
||
|
||
export type TokensMutation = { __typename?: 'Mutation', retoolToken: { __typename?: 'TokenRef', token: string } }; | ||
|
||
|
||
export const Tokens = gql` | ||
mutation Tokens($input: retoolToken!) { | ||
retoolToken(input: $input) { | ||
token | ||
} | ||
} | ||
`; |
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,5 @@ | ||
mutation Tokens($input: retoolToken!) { | ||
retoolToken(input: $input) { | ||
token | ||
} | ||
} |
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,74 @@ | ||
import { faker } from "@faker-js/faker"; | ||
import { decode, verify } from "@tsndr/cloudflare-worker-jwt"; | ||
import { it, describe, assert } from "vitest"; | ||
|
||
import { USER } from "~/datasources/db/users"; | ||
import { executeGraphqlOperation, insertUser } from "~/tests/fixtures"; | ||
|
||
import { | ||
Tokens, | ||
TokensMutation, | ||
TokensMutationVariables, | ||
} from "./token.generated"; | ||
|
||
describe("User", () => { | ||
it("Should create a token", async () => { | ||
const retoolToken = faker.string.alphanumeric(10); | ||
const encoder = faker.string.alphanumeric(10); | ||
const user1 = await insertUser({ | ||
isRetoolEnabled: true, | ||
isSuperAdmin: true, | ||
isEmailVerified: true, | ||
}); | ||
const response = await executeGraphqlOperation< | ||
TokensMutation, | ||
TokensMutationVariables | ||
>( | ||
{ | ||
document: Tokens, | ||
variables: { | ||
input: { | ||
authToken: retoolToken, | ||
userEmail: user1.email, | ||
}, | ||
}, | ||
}, | ||
{ | ||
RETOOL_AUTHENTICATION_TOKEN: retoolToken, | ||
SUPABASE_JWT_ENCODER: encoder, | ||
}, | ||
); | ||
|
||
assert.equal(response.errors, undefined); | ||
|
||
const token = response.data?.retoolToken?.token; | ||
|
||
assert.exists(token); | ||
|
||
if (!token) { | ||
throw new Error("Token not found"); | ||
} | ||
|
||
const verified = await verify(token, encoder); | ||
|
||
assert.exists(verified); | ||
|
||
assert.equal(verified, true); | ||
|
||
const decodedToken = decode<{ | ||
user_metadata: USER; | ||
}>(token); | ||
|
||
const userTokenData = decodedToken?.payload?.user_metadata; | ||
|
||
assert.equal(userTokenData?.email, user1.email); | ||
|
||
assert.equal(userTokenData?.isSuperAdmin, true); | ||
|
||
assert.equal(userTokenData?.isRetoolEnabled, true); | ||
|
||
assert.equal(userTokenData?.isEmailVerified, true); | ||
|
||
assert.equal(userTokenData?.id, user1.id); | ||
}); | ||
}); |
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