Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
R-Sandor committed Sep 8, 2024
1 parent 2ade8cd commit 2bb3223
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 54 deletions.
22 changes: 18 additions & 4 deletions .github/workflows/sonar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ on:
pull_request:
types: [opened, synchronize, reopened]
jobs:
sonarCloudServer:
name: sonarCloudServer
sonarQubeServer:
name: SonarQube Server
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -20,7 +20,7 @@ jobs:
uses: actions/setup-java@v4
with:
java-version: 17
distribution: 'zulu' # see if this makes a difference on the integration tests.
distribution: 'temurin'
- name: Cache SonarQube packages
uses: actions/cache@v4
with:
Expand All @@ -33,11 +33,25 @@ jobs:
path: ~/.gradle/caches
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }}
restore-keys: ${{ runner.os }}-gradle
- name: sonarQube Scan 1
- name: Scan server
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} # analysis token associated to your project
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
run: |
cd server/
./scripts/createServerKeys.sh
./gradlew build sonar --info
sonarQubeFrontend:
name: sonarQube Frontend
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Scan Frontend
uses: sonarsource/sonarqube-scan-action@master
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN_FRONTEND }} # analysis token associated to your project
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
with:
projectBaseDir: frontend/
98 changes: 51 additions & 47 deletions frontend/__tests__/account/resetPassword/passwordResetPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,63 +6,67 @@ import axios from "axios";
import MockAdapter from "axios-mock-adapter";
import { act } from "react-dom/test-utils";
import { beforeEach, beforeAll, vi, describe, it, expect } from "vitest";
import { debug } from "vitest-preview";
const user = userEvent.setup();

beforeEach(async () => {
await act(async () => {
render(<PasswordReset />);
});
await act(async () => {
render(<PasswordReset />);
});
});

beforeAll(() => {
vi.mock("next/navigation", async (importOriginal) => {
const actual = (await importOriginal()) as Object;
return {
...actual,
useRouter: vi.fn(() => ({
push: vi.fn(),
})),
// giberish token
usePathname: vi.fn().mockImplementation(() => "/account/login/"),
};
});
vi.mock("next/navigation", async (importOriginal) => {
const actual = (await importOriginal()) as Object;
return {
...actual,
useRouter: vi.fn(() => ({
push: vi.fn(),
})),
// giberish token
usePathname: vi.fn().mockImplementation(() => "/account/login/"),
};
});
});

describe('User attempts reset password:', () => {
it('User enters invalid email', async () => {
const email = screen.getByPlaceholderText(/email/i);
await act(async () => {
await user.type(email, "ajacobs")
await clickAway(user)
})
expect(screen.getByText("Invalid email")).toBeInTheDocument()
await act(async () => {
await user.clear(email)
})
expect(screen.getByText("Required")).toBeInTheDocument()
it('User enters invalid email', async () => {
const email = screen.getByPlaceholderText(/email/i);
await act(async () => {
await user.type(email, "ajacobs")
await clickAway(user)
})
expect(screen.getByText("Invalid email")).toBeInTheDocument()
await act(async () => {
await user.clear(email)
})
it('User enters an email that does not exist.', async () => {
const email = screen.getByPlaceholderText(/email/i);
expect(screen.getByText("Required")).toBeInTheDocument()
})
it('User enters an email that does not exist.', async () => {
const email = screen.getByPlaceholderText(/email/i);

// This sets the mock adapter on the default instance
let mock = new MockAdapter(axios);
mock.onPost().reply(400, "User does not exist")
await act(async () => {
await user.type(email, "[email protected]")
await user.click(screen.getByText(/submit/i))
})
await waitFor(() => {
expect(screen.getByText("User does not exist")).toBeInTheDocument();
});
// This sets the mock adapter on the default instance
let mock = new MockAdapter(axios);
mock.onPost().reply(400, {
error: 'User does not exist'
})
await act(async () => {
await user.type(email, "[email protected]")
await user.click(screen.getByText(/submit/i))
})
it('User requests valid password reset for email.', async () => {
const email = screen.getByPlaceholderText(/email/i);
// This sets the mock adapter on the default instance
let mock = new MockAdapter(axios);
mock.onPost().reply(200, "Password reset sent")
await act(async () => {
await user.type(email, "[email protected]")
await user.click(screen.getByText(/submit/i))
})
debug();
await waitFor(() => {
expect(screen.getByText("User does not exist")).toBeInTheDocument();
});
})
it('User requests valid password reset for email.', async () => {
const email = screen.getByPlaceholderText(/email/i);
// This sets the mock adapter on the default instance
let mock = new MockAdapter(axios);
mock.onPost().reply(200, "Password reset sent")
await act(async () => {
await user.type(email, "[email protected]")
await user.click(screen.getByText(/submit/i))
})
})
})
})
7 changes: 4 additions & 3 deletions frontend/app/account/resetPassword/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,19 @@ export default function Page() {
useEffect(() => {
if (submitSuccess) {
setSubmitMessage("Reset token has been sent to your email.");
}
}, [submitSuccess]);
}
}, [submitSuccess, submitMessage]);

const handleOnSubmit = async (forgot: ForgotPasswordRequest, actions: any) => {
axios.post(resetUrl + "?email=" + forgot.email)
.then((response) => {
if (response.status == 200)
setSubmitSuccess(true);
}).catch((rejected) => {
console.log("REJECTED")
setSubmitMessage(rejected.response.data.error);
setSubmitSuccess(false);
actions.resetForm();
// actions.resetForm();
})
};
return (
Expand Down

0 comments on commit 2bb3223

Please sign in to comment.