Skip to content

Commit

Permalink
read the requirements proprly - existing users not duplicate in CSV
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiestephenkerr committed Feb 6, 2024
1 parent ffa92d8 commit c680da1
Show file tree
Hide file tree
Showing 5 changed files with 409 additions and 192 deletions.
180 changes: 101 additions & 79 deletions src/pages/users/BulkUserUpload/UsersToUploadSummary.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,45 @@ import {createMemoryHistory} from "history";
import {Router} from "react-router";
import { ImportUser } from "../../../../Interfaces";
import UsersToUploadSummary from "./UsersToUploadSummary";
import { getAllRoles } from "../../../utilities/http";
import { UserRole } from "blaise-api-node-client";
import { getAllRoles, getAllUsers } from "../../../utilities/http";
import { User, UserRole } from "blaise-api-node-client";

// set global vars
let view:RenderResult;

// set mocks
type getRolesListResponse = [boolean, UserRole[]];
type getUsersListResponse = [boolean, User[]];

jest.mock("../../../utilities/http");

const getAllRolesMock = getAllRoles as jest.Mock<Promise<getRolesListResponse>>;
const getAllUsersMock = getAllUsers as jest.Mock<Promise<getUsersListResponse>>;

describe("Upload summary tests", () => {
const importedUsers: ImportUser[] = [
{
name:"Jamie",
password:"pass",
role:"BDSS",
valid:false,
warnings:[]
},
{
name:"Rob",
password:"pass2",
role:"DST",
valid:false,
warnings:[]
},
{
name:"Rich",
password:"pass",
role:"BDSS",
valid:false,
warnings:[]
}
];

const roles: getRolesListResponse = [
true,
Expand All @@ -34,42 +61,25 @@ describe("Upload summary tests", () => {
}
]];

const existingUsers: getUsersListResponse = [
true,
[]
];

beforeEach(() => {
getAllRolesMock.mockImplementation(() => Promise.resolve(roles));
getAllUsersMock.mockImplementation(() => Promise.resolve(existingUsers));
});

it("Upload summary pages for three valid users matches Snapshot", async () => {
it("Upload summary pages for valid imported users matches Snapshot", async () => {
//arrange
const userList: ImportUser[] = [
{
name:"Jamie",
password:"pass",
role:"BDSS",
valid:false,
warnings:[]
},
{
name:"Rob",
password:"pass2",
role:"DST",
valid:false,
warnings:[]
},
{
name:"Rich",
password:"pass",
role:"BDSS",
valid:false,
warnings:[]
}
];

// act
await act(async () => {
const history = createMemoryHistory();
view = render(
<Router history={history}>
<UsersToUploadSummary usersToImport={userList} uploadUsers={() => {return;}}/>
<UsersToUploadSummary usersToImport={importedUsers} uploadUsers={() => {return;}}/>
</Router>
);
});
Expand All @@ -81,39 +91,15 @@ describe("Upload summary tests", () => {

});

it("Upload summary pages for three valid users displays correct summary", async () => {
it("Upload summary pages for valid imported users displays correct summary", async () => {
//arrange
const userList: ImportUser[] = [
{
name:"Jamie",
password:"pass",
role:"BDSS",
valid:false,
warnings:[]
},
{
name:"Rob",
password:"pass2",
role:"DST",
valid:false,
warnings:[]
},
{
name:"Rich",
password:"pass",
role:"BDSS",
valid:false,
warnings:[]
}
];

// act
// act
await act(async () => {
const history = createMemoryHistory();
view = render(
<Router history={history}>
<UsersToUploadSummary usersToImport={userList} uploadUsers={() => {return;}}/>
<UsersToUploadSummary usersToImport={importedUsers} uploadUsers={() => {return;}}/>
</Router>
);
});
Expand All @@ -140,7 +126,7 @@ describe("Upload summary tests", () => {

it("Upload summary pages for two valid and one invalid users matches Snapshot", async () => {
//arrange
const userList: ImportUser[] = [
const invalidImportedUsers: ImportUser[] = [
{
name:"Jamie",
password:"pass",
Expand Down Expand Up @@ -169,7 +155,7 @@ describe("Upload summary tests", () => {
const history = createMemoryHistory();
view = render(
<Router history={history}>
<UsersToUploadSummary usersToImport={userList} uploadUsers={() => {return;}}/>
<UsersToUploadSummary usersToImport={invalidImportedUsers} uploadUsers={() => {return;}}/>
</Router>
);
});
Expand All @@ -182,7 +168,7 @@ describe("Upload summary tests", () => {

it("Upload summary pages for two valid and one invalid users displays correct summary", async () => {
//arrange
const userList: ImportUser[] = [
const invalidImportedUsers: ImportUser[] = [
{
name:"Jamie",
password:"pass",
Expand Down Expand Up @@ -211,7 +197,7 @@ describe("Upload summary tests", () => {
const history = createMemoryHistory();
view = render(
<Router history={history}>
<UsersToUploadSummary usersToImport={userList} uploadUsers={() => {return;}}/>
<UsersToUploadSummary usersToImport={invalidImportedUsers} uploadUsers={() => {return;}}/>
</Router>
);
});
Expand All @@ -236,38 +222,56 @@ describe("Upload summary tests", () => {
expect(user3Summary).toHaveTextContent("Valid User");
});

it("Upload summary pages for two users with the same name matches Snapshot", async () => {
it("Upload summary pages for an imported users that already exist matches Snapshot", async () => {
//arrange
const userList: ImportUser[] = [
const importedUsersIncludingExisting: ImportUser[] = [
{
name:"Jamie",
name:"Jamie", // user already exists
password:"pass",
role:"BDSS",
valid:false,
warnings:[]
},
{
name:"Rich",
password:"pass",
role:"BDSS",
name:"Rob",
password:"pass2",
role:"DST",
valid:false,
warnings:[]
},
{
name:"Jamie",
password:"pass",
name:"Rich", // user already exists
password:"pass3",
role:"BDSS",
valid:false,
warnings:[]
}
];

const matchingExistingUsers: getUsersListResponse = [
true,
[{
name:"Jamie",
role:"BDSS",
serverParks:[],
defaultServerPark:""
},
{
name:"Rich",
role:"BDSS",
serverParks:[],
defaultServerPark:""
}]
];

getAllUsersMock.mockImplementation(() => Promise.resolve(matchingExistingUsers));

// act
await act(async () => {
const history = createMemoryHistory();
view = render(
<Router history={history}>
<UsersToUploadSummary usersToImport={userList} uploadUsers={() => {return;}}/>
<UsersToUploadSummary usersToImport={importedUsersIncludingExisting} uploadUsers={() => {return;}}/>
</Router>
);
});
Expand All @@ -280,36 +284,54 @@ describe("Upload summary tests", () => {

it("Upload summary pages for two users with the same name displays correct summary", async () => {
//arrange
const userList: ImportUser[] = [
const importedUsersIncludingExisting: ImportUser[] = [
{
name:"Jamie",
name:"Jamie", // user already exists
password:"pass",
role:"BDSS",
valid:false,
warnings:[]
},
{
name:"Rich",
password:"pass",
role:"BDSS",
name:"Rob",
password:"pass2",
role:"DST",
valid:false,
warnings:[]
},
{
name:"Jamie",
password:"pass",
name:"Rich", // user already exists
password:"pass3",
role:"BDSS",
valid:false,
warnings:[]
}
];

const matchingExistingUsers: getUsersListResponse = [
true,
[{
name:"Jamie",
role:"BDSS",
serverParks:[],
defaultServerPark:""
},
{
name:"Rich",
role:"BDSS",
serverParks:[],
defaultServerPark:""
}]
];

getAllUsersMock.mockImplementation(() => Promise.resolve(matchingExistingUsers));

// act
await act(async () => {
const history = createMemoryHistory();
view = render(
<Router history={history}>
<UsersToUploadSummary usersToImport={userList} uploadUsers={() => {return;}}/>
<UsersToUploadSummary usersToImport={importedUsersIncludingExisting} uploadUsers={() => {return;}}/>
</Router>
);
});
Expand All @@ -321,16 +343,16 @@ describe("Upload summary tests", () => {
const user1Summary = view.getByTestId("user-table-row-0");
expect(user1Summary).toHaveTextContent("Jamie");
expect(user1Summary).toHaveTextContent("BDSS");
expect(user1Summary).toHaveTextContent("User exists multiple times");
expect(user1Summary).toHaveTextContent("User already exists");

const user2Summary = view.getByTestId("user-table-row-1");
expect(user2Summary).toHaveTextContent("Jamie");
expect(user2Summary).toHaveTextContent("Rich");
expect(user2Summary).toHaveTextContent("BDSS");
expect(user2Summary).toHaveTextContent("User exists multiple times");
expect(user2Summary).toHaveTextContent("User already exists");

const user3Summary = view.getByTestId("user-table-row-2");
expect(user3Summary).toHaveTextContent("Rich");
expect(user3Summary).toHaveTextContent("BDSS");
expect(user3Summary).toHaveTextContent("Rob");
expect(user3Summary).toHaveTextContent("DST");
expect(user3Summary).toHaveTextContent("Valid User");

});
Expand Down
11 changes: 3 additions & 8 deletions src/pages/users/BulkUserUpload/UsersToUploadSummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import {ErrorBoundary, ONSPanel} from "blaise-design-system-react-components";
import {ImportUser} from "../../../../Interfaces";
import Confirmation from "./Confirmation";
import converter from "number-to-words";
import { getAllRoles } from "../../../utilities/http";
import { validateUsers } from "../../../utilities/validation/userValidation";
import { validateImportedUsers } from "../../../utilities/validation/userValidation";

interface Props {
usersToImport: ImportUser[]
Expand All @@ -20,13 +19,9 @@ function UsersToUploadSummary({usersToImport, uploadUsers}: Props): ReactElement
setupUserList().then(() => {return;});
}, []);

async function setupUserList() {
async function setupUserList() {
setListError("Loading ...");

const [success, roleList] = await getAllRoles();
if (success) {
validateUsers(usersToImport, roleList);
}
await validateImportedUsers(usersToImport);

let noValid = 0;
usersToImport.map((user: ImportUser) => {
Expand Down
Loading

0 comments on commit c680da1

Please sign in to comment.