Securized volunteerListGet API

This commit is contained in:
pikiou
2022-04-21 13:15:06 +02:00
parent 117311e6d2
commit 99700e4474
6 changed files with 26 additions and 90 deletions

View File

@@ -21,7 +21,7 @@ export const useTeamAssign = (): [any, any] => {
volunteer: volunteer.id,
team: volunteer.team === teamId ? 0 : teamId,
})
refreshVolunteers()
refreshVolunteers(jwtToken)
},
[save, refreshVolunteers, jwtToken]
)

View File

@@ -23,7 +23,12 @@ const expressAccessor = new ExpressAccessors<VolunteerWithoutId, Volunteer>(
translationVolunteer
)
export const volunteerListGet = expressAccessor.listGet()
export const volunteerListGet = expressAccessor.get(async (list, _body, id) => {
if (id <= 0) {
throw Error(`L'accès est réservé aux utilisateurs identifiés`)
}
return list
})
export const volunteerSet = expressAccessor.set()
export const volunteerDiscordId = expressAccessor.get(async (list, body, id) => {
@@ -174,8 +179,8 @@ async function sendForgetEmail(email: string, password: string): Promise<void> {
to: email,
from: "contact@parisestludique.fr",
subject: "Nouveau mot de passe pour le site bénévole de Paris est Ludique",
text: `Voici le nouveau mot de passe : ${password}\nL'ancien fonctionne encore, si tu t'en rappelles.`,
html: `Voici le nouveau mot de passe : <strong>${password}</strong><br />L'ancien fonctionne encore, si tu t'en rappelles.`,
text: `Voici le nouveau mot de passe : ${password}\nPour te connecter à https://fo.parisestludique.fr`,
html: `Voici le nouveau mot de passe : <strong>${password}</strong>\nPour te connecter à <a href="https://fo.parisestludique.fr">https://fo.parisestludique.fr</a>`,
}
await sgMail.send(msg)
}

View File

@@ -90,7 +90,7 @@ app.post("/PostulantAdd", postulantAdd)
app.post("/VolunteerPartialAdd", volunteerPartialAdd)
app.post("/VolunteerLogin", volunteerLogin)
app.post("/VolunteerForgot", volunteerForgot)
app.get("/VolunteerListGet", volunteerListGet)
app.get("/VolunteerListGet", secure as RequestHandler, volunteerListGet)
// Secured APIs
app.get("/AnnouncementListGet", secure as RequestHandler, announcementListGet)

View File

@@ -12,7 +12,7 @@ import {
const serviceAccessors = new ServiceAccessors<VolunteerWithoutId, Volunteer>(elementName)
export const volunteerListGet = serviceAccessors.listGet()
export const volunteerListGet = serviceAccessors.securedListGet()
export const volunteerDiscordIdGet = serviceAccessors.securedCustomGet<[number]>("DiscordId")
export const volunteerPartialAdd = serviceAccessors.customPost<[Partial<Volunteer>]>("PartialAdd")
export const volunteerSet = serviceAccessors.set()

View File

@@ -1,79 +0,0 @@
import axios from "axios"
import _ from "lodash"
import mockStore from "../../utils/mockStore"
import volunteerList, {
initialState,
getRequesting,
getSuccess,
getFailure,
fetchVolunteerList,
} from "../volunteerList"
import { Volunteer, volunteerExample } from "../../services/volunteers"
jest.mock("axios")
const mockData: Volunteer[] = [volunteerExample]
const mockError = "Oops! Something went wrong."
describe("volunteerList reducer", () => {
it("should handle initial state", () => {
// @ts-expect-error
expect(volunteerList(undefined, {})).toEqual(initialState)
})
it("should handle requesting correctly", () => {
expect(volunteerList(undefined, { type: getRequesting.type })).toEqual({
readyStatus: "request",
ids: [],
entities: {},
})
})
it("should handle success correctly", () => {
expect(volunteerList(undefined, { type: getSuccess.type, payload: mockData })).toEqual({
...initialState,
readyStatus: "success",
ids: _.map(mockData, "id"),
entities: _.keyBy(mockData, "id"),
})
})
it("should handle failure correctly", () => {
expect(volunteerList(undefined, { type: getFailure.type, payload: mockError })).toEqual({
...initialState,
readyStatus: "failure",
error: mockError,
})
})
})
describe("volunteerList action", () => {
it("fetches volunteer list successful", async () => {
const { dispatch, getActions } = mockStore()
const expectedActions = [
{ type: getRequesting.type, payload: undefined },
{ type: getSuccess.type, payload: mockData },
]
// @ts-expect-error
axios.get.mockResolvedValue({ data: mockData })
await dispatch(fetchVolunteerList())
expect(getActions()).toEqual(expectedActions)
})
it("fetches volunteer list failed", async () => {
const { dispatch, getActions } = mockStore()
const expectedActions = [
{ type: getRequesting.type },
{ type: getFailure.type, payload: mockError },
]
// @ts-expect-error
axios.get.mockRejectedValue({ message: mockError })
await dispatch(fetchVolunteerList())
expect(getActions()).toEqual(expectedActions)
})
})

View File

@@ -42,13 +42,23 @@ export const fetchVolunteerList = elementListFetch(
const shouldFetchVolunteerList = (state: AppState) => state.volunteerList.readyStatus !== "success"
export const fetchVolunteerListIfNeed = (): AppThunk => (dispatch, getState) => {
if (shouldFetchVolunteerList(getState())) return dispatch(fetchVolunteerList())
export const fetchVolunteerListIfNeed =
(id = 0): AppThunk =>
(dispatch, getState) => {
let jwt = ""
return null
}
if (!id) {
;({ jwt, id } = getState().auth)
}
if (shouldFetchVolunteerList(getState())) return dispatch(fetchVolunteerList(jwt))
export const refreshVolunteerList = (): AppThunk => (dispatch) => dispatch(fetchVolunteerList())
return null
}
export const refreshVolunteerList =
(jwt: string): AppThunk =>
(dispatch) =>
dispatch(fetchVolunteerList(jwt))
export const selectVolunteerListState = (state: AppState): EntitiesRequest<Volunteer> =>
state.volunteerList