Skip to content

Commit

Permalink
fix: General bugfixes and improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
kidunot89 committed Jul 3, 2023
1 parent 3c62729 commit 34c5385
Show file tree
Hide file tree
Showing 95 changed files with 5,522 additions and 4,590 deletions.
1 change: 1 addition & 0 deletions backend/config/application.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
Config::define('SECURE_AUTH_SALT', env('SECURE_AUTH_SALT'));
Config::define('LOGGED_IN_SALT', env('LOGGED_IN_SALT'));
Config::define('NONCE_SALT', env('NONCE_SALT'));
Config::define('GRAPHQL_JWT_AUTH_SECRET_KEY', env('GRAPHQL_JWT_AUTH_SECRET_KEY'));

/**
* Custom Settings
Expand Down
48 changes: 17 additions & 31 deletions frontend/app/[category]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { Shop } from '@woographql/server/Shop';
import { ShopProvider } from '@woographql/client/ShopProvider';
import { SessionProvider } from '@woographql/client/SessionProvider';

import {
fetchAllProducts,
fetchAllCategories,
fetchAllColors,
} from '../page';
fetchProducts,
fetchColors,
} from '@woographql/graphql';

import { Shop } from '@woographql/server/Shop';
import { ShopProvider } from '@woographql/client/ShopProvider';
export interface CategoryPageProps {
params: {
category: string
Expand All @@ -18,33 +15,22 @@ export default async function CategoryPage({ params }: CategoryPageProps) {
const { category } = params;

if (!category) return (
<main className="w-full">
<h1>Page not found</h1>
</main>
<h1>Page not found</h1>
);

const products = await fetchAllProducts({ category: category });
const categories = await fetchAllCategories() || [];
const colors = await fetchAllColors() || [];
const products = await fetchProducts(1, 0, { category: category });
const colors = await fetchColors(1) || [];

if (!products) return (
<main className="w-full">
<h1>Page not found</h1>
</main>
if (!products || products.length === 0) return (
<h1>{`The ${category} category does not exist. Please check URL and try again.`}</h1>
);

return (
<SessionProvider>
<main className="w-full">
<h1 className="max-w-screen-lg text-2xl font-bold font-serif mx-auto mb-8">Shop</h1>
<ShopProvider allProducts={products}>
<Shop
products={products}
categories={categories}
colors={colors}
/>
</ShopProvider>
</main>
</SessionProvider>
)
<ShopProvider allProducts={products}>
<Shop
products={products}
colors={colors}
/>
</ShopProvider>
);
}
290 changes: 290 additions & 0 deletions frontend/app/api/auth/route.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,290 @@
/**
* @jest-environment node
*/
import type { RequestMethod } from 'node-mocks-http';

import { getClient } from '@woographql/graphql';
import { GET, POST } from './route';
import { GraphQLClient } from 'graphql-request';

jest.mock('graphql', () => ({
print: jest.fn(() => 'print'),
}));
jest.mock('@woographql/graphql', () => ({
getClient: jest.fn(),
}));

const mockGetClient = getClient as jest.MockedFunction<typeof getClient>;

describe( '/api/auth API Endpoint', () => {
process.env.GRAPHQL_ENDPOINT = 'http://localhost/graphql';
const mockRequest = (method: RequestMethod = 'GET', body?: unknown) => {
const request = new Request('http://localhost/api/support', {
method,
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
body: body ? JSON.stringify(body) : undefined,
});
return { request };
};

it(`should return a 500 error from a GET request when GetSessionQuery returned invalid
response`, async () => {
const { request } = mockRequest();
mockGetClient.mockImplementationOnce(() => ({
rawRequest: jest.fn(() => ({
data: {
cart: null,
customer: {},
},
headers: {
get: jest.fn(() => 'sessionToken'),
},
status: 200,
extensions: [],
errors: [],
})) as unknown,
} as unknown as GraphQLClient));

let response = await GET(request);
expect(response.status).toBe(500);
expect(response.ok).toBeFalsy();

let json = await response.json();
expect(json.errors.message).toBe('Failed to retrieve session credentials.');
expect(json.sessionToken).toBeUndefined();
expect(json.authToken).toBeUndefined();

mockGetClient.mockImplementationOnce(() => ({
rawRequest: jest.fn(() => ({
data: {
cart: {},
customer: null,
},
headers: {
get: jest.fn(() => 'sessionToken'),
},
status: 200,
extensions: [],
errors: [],
})) as unknown,
} as unknown as GraphQLClient));

response = await GET(request);
expect(response.status).toBe(500);
expect(response.ok).toBeFalsy();

json = await response.json();
expect(json.errors.message).toBe('Failed to retrieve session credentials.');
expect(json.sessionToken).toBeUndefined();
expect(json.authToken).toBeUndefined();

mockGetClient.mockImplementationOnce(() => ({
rawRequest: jest.fn(() => ({
data: {
cart: {},
customer: {},
},
headers: {
get: jest.fn(() => null),
},
status: 200,
extensions: [],
errors: [],
})) as unknown,
} as unknown as GraphQLClient));

response = await GET(request);
expect(response.status).toBe(500);
expect(response.ok).toBeFalsy();

json = await response.json();
expect(json.errors.message).toBe('Failed to retrieve session credentials.');
expect(json.sessionToken).toBeUndefined();
expect(json.authToken).toBeUndefined();
});

it(`should return a 200 response from a GET Request when proper response for
"GetSessionQuery" is returned`, async () => {
const { request } = mockRequest();
mockGetClient.mockImplementationOnce(() => ({
rawRequest: jest.fn(() => ({
data: {
cart: {},
customer: {},
},
headers: {
get: jest.fn(() => 'sessionToken'),
},
})),
} as unknown as GraphQLClient));

const response = await GET(request);
expect(response.status).toBe(200);
expect(response.ok).toBeTruthy();

let json = await response.json();
expect(json.sessionToken).toBe('sessionToken');
expect(json.authToken).toBeUndefined();
expect(json.errors).toBeUndefined();
});

it('should return a 500 error from a POST request when no refresh or auth token provided.', async () => {
const { request } = mockRequest('POST', {});

const response = await POST(request);
expect(response.status).toBe(500);
expect(response.ok).toBeFalsy();

const json = await response.json();
expect(json.errors.message).toBe('No refresh token provided');
});

it(`should return a 500 error from a POST request when "RefreshAuthTokenMutation"
returns an invalid response.`, async () => {
const { request } = mockRequest('POST', { refreshToken: 'refreshToken' });
const mockSetHeaders = jest.fn();
mockGetClient.mockImplementationOnce(() => ({
setHeaders: mockSetHeaders,
request: jest.fn(() => ({
refreshJwtAuthToken: {
authToken: null,
},
})),
} as unknown as GraphQLClient));

const response = await POST(request);
expect(response.status).toBe(500);
expect(response.ok).toBeFalsy();
expect(mockSetHeaders).toBeCalledTimes(1);

const json = await response.json();
expect(json.errors.message).toBe('Failed to retrieve auth token.');
});

it(`should return a 500 error from a POST request when "GetSessionQuery" returns an
invalid response after using a new "authToken".`, async () => {
const { request } = mockRequest('POST', { refreshToken: 'refreshToken' });
const mockSetHeaders = jest.fn();
mockGetClient.mockImplementationOnce(() => ({
request: jest.fn(() => ({
refreshJwtAuthToken: {
authToken: 'authToken',
},
})),
setHeaders: mockSetHeaders,
rawRequest: jest.fn(() => ({
data: {
cart: {},
customer: {
sessionToken: null,
},
},
headers: {},
})),
} as unknown as GraphQLClient));

const response = await POST(request);
expect(response.status).toBe(500);
expect(response.ok).toBeFalsy();
expect(mockSetHeaders).toBeCalledTimes(2);

const json = await response.json();
expect(json.errors.message).toBe('Failed to validate auth token.');
});

it(`should return a 200 response from a POST request when "GetSessionQuery" returns a
valid response after using a new "authToken".`, async () => {
const { request } = mockRequest('POST', { refreshToken: 'refreshToken' });
const mockSetHeaders = jest.fn();
mockGetClient.mockImplementationOnce(() => ({
request: jest.fn(() => ({
refreshJwtAuthToken: {
authToken: 'authToken',
},
})),
setHeaders: mockSetHeaders,
rawRequest: jest.fn(() => ({
data: {
cart: {},
customer: {
sessionToken: 'sessionToken',
},
},
headers: {
get: jest.fn(() => 'sessionTokenFromResponseHeader'),
},
})),
} as unknown as GraphQLClient));

const response = await POST(request);
expect(response.status).toBe(200);
expect(response.ok).toBeTruthy();
expect(mockSetHeaders).toBeCalledTimes(2);

const json = await response.json();
expect(json.sessionToken).toBe('sessionTokenFromResponseHeader');
expect(json.authToken).toBe('authToken');
expect(json.errors).toBeUndefined();
});

it(`should return a 500 error from a POST request when "GetSessionQuery" returns an
invalid response after using an existing "authToken" from the request body.`, async () => {
const { request } = mockRequest('POST', { authToken: 'authToken' });
const mockSetHeaders = jest.fn();
mockGetClient.mockImplementationOnce(() => ({
setHeaders: mockSetHeaders,
rawRequest: jest.fn(() => ({
data: {
cart: {},
customer: {
sessionToken: null,
},
},
headers: {
get: jest.fn(() => 'sessionTokenFromResponseHeader'),
},
})),
} as unknown as GraphQLClient));

const response = await POST(request);
expect(response.status).toBe(500);
expect(response.ok).toBeFalsy();
expect(mockSetHeaders).toBeCalledTimes(1);

const json = await response.json();
expect(json.errors.message).toBe('Failed to validate auth token.');
});

it(`should return a 200 response from a POST request when "GetSessionQuery" returns a
valid response after using an existing "authToken" from the request body.`, async () => {
const { request } = mockRequest('POST', { authToken: 'authToken' });
const mockSetHeaders = jest.fn();
mockGetClient.mockImplementationOnce(() => ({
setHeaders: mockSetHeaders,
rawRequest: jest.fn(() => ({
data: {
cart: {},
customer: {
sessionToken: 'sessionToken',
},
},
headers: {
get: jest.fn(() => null),
},
})),
} as unknown as GraphQLClient));

const response = await POST(request);
expect(response.status).toBe(200);
expect(response.ok).toBeTruthy();
expect(mockSetHeaders).toBeCalledTimes(1);

const json = await response.json();
expect(json.sessionToken).toBe('sessionToken');
expect(json.authToken).toBe('authToken');
expect(json.errors).toBeUndefined();
});
});
Loading

0 comments on commit 34c5385

Please sign in to comment.