|
| 1 | +// Kiebitz - Privacy-Friendly Appointments |
| 2 | +// Copyright (C) 2021-2021 The Kiebitz Authors |
| 3 | +// README.md contains license information. |
| 4 | + |
| 5 | +import { Status } from "../interfaces" |
| 6 | +import { equal } from "assert" |
| 7 | +import { formatDatetime } from "../helpers/time" |
| 8 | +import { |
| 9 | + backend, |
| 10 | + adminKeys, |
| 11 | + resetDB, |
| 12 | + mediator, |
| 13 | + verifiedProvider, |
| 14 | +} from "../testing/fixtures" |
| 15 | +import { User } from "./" |
| 16 | + |
| 17 | +beforeEach(async function () { |
| 18 | + this.backend = backend() |
| 19 | + |
| 20 | + const keys = await adminKeys() |
| 21 | + // we reset the database |
| 22 | + await resetDB(this.backend, keys) |
| 23 | + // we create a mediator |
| 24 | + const med = await mediator(this.backend, keys) |
| 25 | + // we create an unverified provider |
| 26 | + const vp = await verifiedProvider(this.backend, keys, med) |
| 27 | + |
| 28 | + let date = new Date() |
| 29 | + |
| 30 | + // tomorrow 3 pm |
| 31 | + |
| 32 | + date.setDate(date.getDate() + 1) |
| 33 | + date.setHours(15) |
| 34 | + date.setMinutes(0) |
| 35 | + date.setSeconds(0) |
| 36 | + date.setMilliseconds(0) |
| 37 | + |
| 38 | + var app = await vp.createAppointment(15, "moderna", 5, date.toISOString()) |
| 39 | + |
| 40 | + const publishResult = await vp.publishAppointments([app]) |
| 41 | + |
| 42 | + if (publishResult.status !== Status.Succeeded) |
| 43 | + throw new Error("cannot create appointment") |
| 44 | + |
| 45 | + this.user = new User("main", this.backend) |
| 46 | + // we generate a secret etc. |
| 47 | + await this.user.initialize() |
| 48 | + // we set the queue data |
| 49 | + this.user.queueData = { |
| 50 | + zipCode: "10707", |
| 51 | + } |
| 52 | + // we set the contact data |
| 53 | + this.user.contactData = { |
| 54 | + name: "Max Mustermann", |
| 55 | + } |
| 56 | +}) |
| 57 | + |
| 58 | +describe("User.bookAppointment()", function () { |
| 59 | + it("we should be able to book an appointment", async function () { |
| 60 | + const tokenResult = await this.user.getToken({}) |
| 61 | + |
| 62 | + if (tokenResult.status !== Status.Succeeded) |
| 63 | + throw new Error("cannot get token") |
| 64 | + |
| 65 | + const fromDate = new Date() |
| 66 | + // 24 hours in the future |
| 67 | + const toDate = new Date(new Date().getTime() + 48 * 60 * 60 * 1000) |
| 68 | + const getResult = await this.user.getAppointments({ |
| 69 | + from: formatDatetime(fromDate), |
| 70 | + to: formatDatetime(toDate), |
| 71 | + zipCode: this.user.queueData!.zipCode, |
| 72 | + }) |
| 73 | + |
| 74 | + if (getResult.status !== Status.Succeeded) |
| 75 | + throw new Error("should not fail") |
| 76 | + |
| 77 | + if (getResult.appointments.length !== 1) |
| 78 | + throw new Error("should return one appointment") |
| 79 | + |
| 80 | + const bookResult = await this.user.bookAppointment( |
| 81 | + getResult.appointments[0].appointments[0], |
| 82 | + getResult.appointments[0].provider |
| 83 | + ) |
| 84 | + |
| 85 | + if (bookResult.status !== Status.Succeeded) |
| 86 | + throw new Error("should not fail") |
| 87 | + }) |
| 88 | +}) |
0 commit comments