554 lines
19 KiB
Go
554 lines
19 KiB
Go
package web
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"lektorenplan/domain"
|
|
"lektorenplan/store"
|
|
)
|
|
|
|
// readerPairFixture prepares two linked reader accounts and one future
|
|
// assignment for each of them. The editor logs in after the accounts exist so
|
|
// that the initial key reconciliation grants every reader their own scope.
|
|
type readerPairFixture struct {
|
|
ts *httptest.Server
|
|
st *store.Store
|
|
tenantID string
|
|
editor *http.Client
|
|
anna *http.Client
|
|
markus *http.Client
|
|
annaUser *store.User
|
|
markUser *store.User
|
|
annaPass string
|
|
markPass string
|
|
annaJob domain.Assignment
|
|
markJob domain.Assignment
|
|
annaSvc domain.ServiceOccurrence
|
|
markSvc domain.ServiceOccurrence
|
|
}
|
|
|
|
// httptestServer was intentionally removed; the fixture uses *httptest.Server.
|
|
func newReaderPairFixture(t *testing.T) *readerPairFixture {
|
|
t.Helper()
|
|
ts, st := newTestServer(t)
|
|
ctx := context.Background()
|
|
tenant, err := st.TenantBySlug(ctx, "bad-godesberg")
|
|
if err != nil {
|
|
t.Fatalf("tenant: %v", err)
|
|
}
|
|
annaUser, err := st.CreateUser(ctx, "anna", "Anna Berg", "annaread123",
|
|
store.RoleReader, true, []string{tenant.ID})
|
|
if err != nil {
|
|
t.Fatalf("create anna: %v", err)
|
|
}
|
|
if err := st.SetUserPersonLink(ctx, annaUser.ID, tenant.ID, "p-anna"); err != nil {
|
|
t.Fatalf("link anna: %v", err)
|
|
}
|
|
markusUser, err := st.CreateUser(ctx, "markus", "Markus Weber", "markusread123",
|
|
store.RoleReader, true, []string{tenant.ID})
|
|
if err != nil {
|
|
t.Fatalf("create markus: %v", err)
|
|
}
|
|
if err := st.SetUserPersonLink(ctx, markusUser.ID, tenant.ID, "p-markus"); err != nil {
|
|
t.Fatalf("link markus: %v", err)
|
|
}
|
|
|
|
editor := newClient(t)
|
|
login(t, editor, ts.URL, "editor", "editor123")
|
|
anna := newClient(t)
|
|
login(t, anna, ts.URL, "anna", "annaread123")
|
|
markus := newClient(t)
|
|
login(t, markus, ts.URL, "markus", "markusread123")
|
|
|
|
fixture := &readerPairFixture{
|
|
ts: ts, st: st, tenantID: tenant.ID,
|
|
editor: editor, anna: anna, markus: markus,
|
|
annaUser: annaUser, markUser: markusUser,
|
|
annaPass: "annaread1231", markPass: "markusread1231",
|
|
}
|
|
|
|
// The demo seeds a cure for Markus that would hide the next days. Clear
|
|
// both volunteers so the fixture controls the only absence in a test.
|
|
for _, personID := range []string{"p-anna", "p-markus"} {
|
|
absences, err := st.AbsencesForPerson(ctx, tenant.ID, personID)
|
|
if err != nil {
|
|
t.Fatalf("absences for %s: %v", personID, err)
|
|
}
|
|
for _, absence := range absences {
|
|
if err := st.DeleteAbsence(ctx, tenant.ID, absence.ID); err != nil {
|
|
t.Fatalf("delete absence %s: %v", absence.ID, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
annaDate := domain.AddingDays(1, domain.Today())
|
|
markDate := domain.AddingDays(2, domain.Today())
|
|
annaServiceID, err := st.UpsertService(ctx, tenant.ID, domain.ServiceOccurrence{
|
|
ID: "swap-service-anna", ChurchID: "c-andreas", Date: annaDate,
|
|
TimeMinutes: 9 * 60, Title: "Tauschprobe Anna", Kind: domain.KindSpecial,
|
|
LectorsNeeded: 1, CreatedAt: time.Now().UTC(),
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create anna service: %v", err)
|
|
}
|
|
markServiceID, err := st.UpsertService(ctx, tenant.ID, domain.ServiceOccurrence{
|
|
ID: "swap-service-markus", ChurchID: "c-evergislus", Date: markDate,
|
|
TimeMinutes: 10 * 60, Title: "Tauschprobe Markus", Kind: domain.KindSpecial,
|
|
LectorsNeeded: 1, CreatedAt: time.Now().UTC(),
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create markus service: %v", err)
|
|
}
|
|
fixture.annaJob = domain.Assignment{ID: "swap-assignment-anna", ServiceID: annaServiceID,
|
|
PersonID: "p-anna", Role: domain.RoleLector}
|
|
fixture.markJob = domain.Assignment{ID: "swap-assignment-markus", ServiceID: markServiceID,
|
|
PersonID: "p-markus", Role: domain.RoleLector}
|
|
for _, assignment := range []domain.Assignment{fixture.annaJob, fixture.markJob} {
|
|
if err := st.UpsertAssignment(ctx, tenant.ID, assignment); err != nil {
|
|
t.Fatalf("create assignment %s: %v", assignment.ID, err)
|
|
}
|
|
}
|
|
fixture.annaSvc, _ = serviceByID(t, st, tenant.ID, annaServiceID)
|
|
fixture.markSvc, _ = serviceByID(t, st, tenant.ID, markServiceID)
|
|
return fixture
|
|
}
|
|
|
|
func serviceByID(t *testing.T, st *store.Store, tenantID, id string) (domain.ServiceOccurrence, bool) {
|
|
t.Helper()
|
|
service, err := st.Service(context.Background(), tenantID, id)
|
|
if err != nil {
|
|
return domain.ServiceOccurrence{}, false
|
|
}
|
|
return *service, true
|
|
}
|
|
|
|
// shareContacts lets owner share their address with the given reader accounts.
|
|
func shareContacts(t *testing.T, client *http.Client, base string, viewerIDs ...string) {
|
|
t.Helper()
|
|
page := get(t, client, base+"/team", http.StatusOK)
|
|
form := url.Values{"csrf": {extractCSRF(t, page)}}
|
|
for _, viewerID := range viewerIDs {
|
|
form.Add("viewer", viewerID)
|
|
}
|
|
resp := postForm(t, client, base+"/team/sharing", form)
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != http.StatusSeeOther {
|
|
t.Fatalf("save sharing = %d, want 303", resp.StatusCode)
|
|
}
|
|
}
|
|
|
|
func mutualShares(t *testing.T, f *readerPairFixture) {
|
|
t.Helper()
|
|
shareContacts(t, f.anna, f.ts.URL, f.markUser.ID)
|
|
shareContacts(t, f.markus, f.ts.URL, f.annaUser.ID)
|
|
}
|
|
|
|
func decodeFlash(t *testing.T, resp *http.Response) string {
|
|
t.Helper()
|
|
message := ""
|
|
for _, cookie := range resp.Cookies() {
|
|
if cookie.Name != flashCookie || cookie.Value == "" {
|
|
continue
|
|
}
|
|
data, err := base64.RawURLEncoding.DecodeString(cookie.Value)
|
|
if err != nil {
|
|
t.Fatalf("decode flash: %v", err)
|
|
}
|
|
// A second flash overwrites the first one; the client keeps the last
|
|
// Set-Cookie value.
|
|
message = string(data)
|
|
}
|
|
return message
|
|
}
|
|
|
|
func TestReaderContactSharingAndRevocation(t *testing.T) {
|
|
f := newReaderPairFixture(t)
|
|
|
|
before := get(t, f.markus, f.ts.URL+"/team", http.StatusOK)
|
|
if strings.Contains(before, "anna.berg@example.org") {
|
|
t.Fatal("reader sees an address that was not shared")
|
|
}
|
|
|
|
shareContacts(t, f.anna, f.ts.URL, f.markUser.ID)
|
|
|
|
after := get(t, f.markus, f.ts.URL+"/team", http.StatusOK)
|
|
if !strings.Contains(after, "anna.berg@example.org") {
|
|
t.Fatal("shared address is not visible to the other reader")
|
|
}
|
|
if !strings.Contains(after, "Freigegeben für Sie") {
|
|
t.Fatal("team page is missing the shared-with-me section")
|
|
}
|
|
|
|
// A shared scope survives the login policy pruning.
|
|
if err := f.st.PruneUserWrappers(context.Background(), f.markUser.ID); err != nil {
|
|
t.Fatalf("prune wrappers: %v", err)
|
|
}
|
|
scopes, err := f.st.ScopesForUser(context.Background(), f.markUser.ID)
|
|
if err != nil {
|
|
t.Fatalf("scopes: %v", err)
|
|
}
|
|
if !hasPersonScope(scopes, f.tenantID, "p-anna") {
|
|
t.Fatal("shared scope was pruned although the consent still exists")
|
|
}
|
|
|
|
// Revoking removes the wrapper and the open session loses the key.
|
|
shareContacts(t, f.anna, f.ts.URL)
|
|
|
|
revoked := get(t, f.markus, f.ts.URL+"/team", http.StatusOK)
|
|
if strings.Contains(revoked, "anna.berg@example.org") {
|
|
t.Fatal("address is still visible after revoking the share")
|
|
}
|
|
scopes, err = f.st.ScopesForUser(context.Background(), f.markUser.ID)
|
|
if err != nil {
|
|
t.Fatalf("scopes after revoke: %v", err)
|
|
}
|
|
if hasPersonScope(scopes, f.tenantID, "p-anna") {
|
|
t.Fatal("revoked scope is still wrapped for the reader")
|
|
}
|
|
}
|
|
|
|
func hasPersonScope(scopes []store.KeyScope, tenantID, personID string) bool {
|
|
for _, scope := range scopes {
|
|
if scope.Type == store.ScopePerson && scope.TenantID == tenantID && scope.ID == personID {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func TestReaderRosterChangeNotice(t *testing.T) {
|
|
f := newReaderPairFixture(t)
|
|
|
|
// One review makes the current reader directory known.
|
|
shareContacts(t, f.anna, f.ts.URL, f.markUser.ID)
|
|
page := get(t, f.anna, f.ts.URL+"/team", http.StatusOK)
|
|
if strings.Contains(page, "hat sich geändert") {
|
|
t.Fatal("roster notice appears without a roster change")
|
|
}
|
|
|
|
ctx := context.Background()
|
|
third, err := f.st.CreateUser(ctx, "jonas", "Jonas Schmitz", "jonasread123",
|
|
store.RoleReader, true, []string{f.tenantID})
|
|
if err != nil {
|
|
t.Fatalf("create third reader: %v", err)
|
|
}
|
|
if err := f.st.SetUserPersonLink(ctx, third.ID, f.tenantID, "p-johannes"); err != nil {
|
|
t.Fatalf("link third reader: %v", err)
|
|
}
|
|
|
|
page = get(t, f.anna, f.ts.URL+"/team", http.StatusOK)
|
|
if !strings.Contains(page, "Die Liste der Leserinnen und Leser hat sich geändert.") {
|
|
t.Fatal("persistent roster notice is missing")
|
|
}
|
|
|
|
// The hint is repeated as a login flash on the next sign-in.
|
|
relogin := newClient(t)
|
|
resp, err := relogin.PostForm(f.ts.URL+"/login", url.Values{
|
|
"username": {"anna"}, "password": {f.annaPass},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("relogin: %v", err)
|
|
}
|
|
resp.Body.Close()
|
|
flash := decodeFlash(t, resp)
|
|
if !strings.Contains(flash, "Liste der Lektoren und Kommunionhelfer hat sich geändert") {
|
|
t.Fatalf("login flash = %q, want roster notice", flash)
|
|
}
|
|
|
|
// Acknowledging keeps the existing shares but silences the notice.
|
|
page = get(t, relogin, f.ts.URL+"/team", http.StatusOK)
|
|
csrf := extractCSRF(t, page)
|
|
ack := postForm(t, relogin, f.ts.URL+"/team/sharing/ack", url.Values{"csrf": {csrf}})
|
|
ack.Body.Close()
|
|
if ack.StatusCode != http.StatusSeeOther {
|
|
t.Fatalf("ack = %d, want 303", ack.StatusCode)
|
|
}
|
|
page = get(t, relogin, f.ts.URL+"/team", http.StatusOK)
|
|
if strings.Contains(page, "Die Liste der Leserinnen und Leser hat sich geändert.") {
|
|
t.Fatal("roster notice is still shown after acknowledgement")
|
|
}
|
|
if !f.st.ContactShareExists(context.Background(), f.tenantID, "p-anna", f.markUser.ID) {
|
|
t.Fatal("acknowledging the roster dropped the existing share")
|
|
}
|
|
}
|
|
|
|
func createSwapRequest(t *testing.T, f *readerPairFixture) (requestID, token string) {
|
|
t.Helper()
|
|
page := get(t, f.anna, f.ts.URL+"/swap/new?assignment="+f.markJob.ID, http.StatusOK)
|
|
if !strings.Contains(page, f.markJob.ID) {
|
|
t.Fatalf("request page does not describe the wanted assignment:\n%s", page)
|
|
}
|
|
if !strings.Contains(page, f.annaJob.ID) {
|
|
t.Fatal("request page does not offer the own assignment")
|
|
}
|
|
csrf := extractCSRF(t, page)
|
|
resp := postForm(t, f.anna, f.ts.URL+"/swap/request", url.Values{
|
|
"csrf": {csrf},
|
|
"wanted": {f.markJob.ID},
|
|
"offered": {f.annaJob.ID},
|
|
})
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != http.StatusSeeOther {
|
|
t.Fatalf("create swap = %d, want 303", resp.StatusCode)
|
|
}
|
|
location := resp.Header.Get("Location")
|
|
parsed, err := url.Parse(location)
|
|
if err != nil {
|
|
t.Fatalf("parse redirect %q: %v", location, err)
|
|
}
|
|
requestID = strings.TrimPrefix(parsed.Path, "/swap/requests/")
|
|
token = parsed.Query().Get("token")
|
|
if requestID == "" || token == "" {
|
|
t.Fatalf("redirect %q is missing request id or token", location)
|
|
}
|
|
return requestID, token
|
|
}
|
|
|
|
func TestSwapRequestLifecycle(t *testing.T) {
|
|
f := newReaderPairFixture(t)
|
|
mutualShares(t, f)
|
|
|
|
// The reader sees a request button next to the other reader's assignment.
|
|
detail := get(t, f.anna, f.ts.URL+"/services/"+f.markSvc.ID, http.StatusOK)
|
|
if !strings.Contains(detail, "/swap/new?assignment="+f.markJob.ID) {
|
|
t.Fatal("service view is missing the swap request button")
|
|
}
|
|
|
|
requestID, token := createSwapRequest(t, f)
|
|
|
|
page := get(t, f.anna, f.ts.URL+"/swap/requests/"+requestID+"?token="+url.QueryEscape(token), http.StatusOK)
|
|
if !strings.Contains(page, "markus.weber@example.org") {
|
|
t.Fatal("request page does not offer a mail draft to the target address")
|
|
}
|
|
if strings.Count(page, token) < 2 || !strings.Contains(page, "Annehmen") || !strings.Contains(page, "Ablehnen") {
|
|
t.Fatal("request mail draft is missing the accept and decline links")
|
|
}
|
|
|
|
marks, err := f.st.PendingSwapAssignmentIDs(context.Background(), f.tenantID)
|
|
if err != nil {
|
|
t.Fatalf("pending marks: %v", err)
|
|
}
|
|
if marks[f.annaJob.ID] == "" || marks[f.markJob.ID] == "" {
|
|
t.Fatal("both assignments must be marked as swap-requested")
|
|
}
|
|
plan := get(t, f.anna, f.ts.URL+"/plan?start="+domain.Key(domain.Today())+
|
|
"&end="+domain.Key(domain.AddingDays(10, domain.Today())), http.StatusOK)
|
|
if !strings.Contains(plan, "Tausch angefragt") {
|
|
t.Fatal("plan does not mark the pending swap")
|
|
}
|
|
|
|
// The target can confirm and accept the swap.
|
|
targetPage := get(t, f.markus, f.ts.URL+"/swap/"+token, http.StatusOK)
|
|
if !strings.Contains(targetPage, "Möchten Sie tauschen?") {
|
|
t.Fatal("target does not see the response controls")
|
|
}
|
|
confirm := get(t, f.markus, f.ts.URL+"/swap/"+token+"/accept", http.StatusOK)
|
|
if !strings.Contains(confirm, "Tausch annehmen?") {
|
|
t.Fatal("accept confirmation page is missing")
|
|
}
|
|
csrf := extractCSRF(t, confirm)
|
|
response := postForm(t, f.markus, f.ts.URL+"/swap/"+token+"/respond", url.Values{
|
|
"csrf": {csrf},
|
|
"action": {"accept"},
|
|
})
|
|
response.Body.Close()
|
|
if response.StatusCode != http.StatusSeeOther {
|
|
t.Fatalf("accept = %d, want 303", response.StatusCode)
|
|
}
|
|
|
|
if !personHasAssignment(t, f.st, f.tenantID, "p-anna", f.markSvc.ID) {
|
|
t.Fatal("anna did not receive the wanted assignment")
|
|
}
|
|
if !personHasAssignment(t, f.st, f.tenantID, "p-markus", f.annaSvc.ID) {
|
|
t.Fatal("markus did not receive the offered assignment")
|
|
}
|
|
if assignmentPerson(t, f.st, f.tenantID, f.annaJob.ID) != "p-markus" {
|
|
t.Fatal("offered assignment was not transferred")
|
|
}
|
|
if assignmentPerson(t, f.st, f.tenantID, f.markJob.ID) != "p-anna" {
|
|
t.Fatal("wanted assignment was not transferred")
|
|
}
|
|
|
|
marks, err = f.st.PendingSwapAssignmentIDs(context.Background(), f.tenantID)
|
|
if err != nil {
|
|
t.Fatalf("pending marks after accept: %v", err)
|
|
}
|
|
if marks[f.annaJob.ID] != "" || marks[f.markJob.ID] != "" {
|
|
t.Fatal("accepted swap is still marked as pending")
|
|
}
|
|
|
|
// The target receives a response draft addressed to the requester.
|
|
result := get(t, f.markus, f.ts.URL+"/swap/"+token, http.StatusOK)
|
|
if !strings.Contains(result, "Antwort-E-Mail öffnen") {
|
|
t.Fatal("accepted request has no response mail draft")
|
|
}
|
|
if !strings.Contains(result, "anna.berg@example.org") {
|
|
t.Fatal("response draft does not use the requester address")
|
|
}
|
|
}
|
|
|
|
func TestSwapDeclineKeepsPlan(t *testing.T) {
|
|
f := newReaderPairFixture(t)
|
|
mutualShares(t, f)
|
|
_, token := createSwapRequest(t, f)
|
|
|
|
confirm := get(t, f.markus, f.ts.URL+"/swap/"+token+"/decline", http.StatusOK)
|
|
if !strings.Contains(confirm, "Tausch ablehnen?") {
|
|
t.Fatal("decline confirmation page is missing")
|
|
}
|
|
csrf := extractCSRF(t, confirm)
|
|
response := postForm(t, f.markus, f.ts.URL+"/swap/"+token+"/respond", url.Values{
|
|
"csrf": {csrf},
|
|
"action": {"decline"},
|
|
})
|
|
response.Body.Close()
|
|
if response.StatusCode != http.StatusSeeOther {
|
|
t.Fatalf("decline = %d, want 303", response.StatusCode)
|
|
}
|
|
|
|
if assignmentPerson(t, f.st, f.tenantID, f.annaJob.ID) != "p-anna" {
|
|
t.Fatal("decline changed the offered assignment")
|
|
}
|
|
if assignmentPerson(t, f.st, f.tenantID, f.markJob.ID) != "p-markus" {
|
|
t.Fatal("decline changed the wanted assignment")
|
|
}
|
|
marks, err := f.st.PendingSwapAssignmentIDs(context.Background(), f.tenantID)
|
|
if err != nil {
|
|
t.Fatalf("pending marks: %v", err)
|
|
}
|
|
if marks[f.annaJob.ID] != "" || marks[f.markJob.ID] != "" {
|
|
t.Fatal("declined swap is still marked as pending")
|
|
}
|
|
result := get(t, f.markus, f.ts.URL+"/swap/"+token, http.StatusOK)
|
|
if !strings.Contains(result, "Abgelehnt") && !strings.Contains(result, "abgelehnt") {
|
|
t.Fatal("result page does not show the declined state")
|
|
}
|
|
if !strings.Contains(result, "Antwort-E-Mail öffnen") {
|
|
t.Fatal("declined request has no response mail draft")
|
|
}
|
|
}
|
|
|
|
func TestAcceptRevalidatesAbsence(t *testing.T) {
|
|
f := newReaderPairFixture(t)
|
|
mutualShares(t, f)
|
|
_, token := createSwapRequest(t, f)
|
|
|
|
ctx := context.Background()
|
|
if err := f.st.UpsertAbsence(ctx, f.tenantID, domain.Absence{
|
|
ID: "late-absence", PersonID: "p-markus",
|
|
Start: f.annaSvc.Date, End: f.annaSvc.Date, Reason: "Kurzfristig krank",
|
|
}); err != nil {
|
|
t.Fatalf("create absence: %v", err)
|
|
}
|
|
|
|
if _, err := f.st.RespondSwapRequest(ctx, f.tenantID, token, f.markUser.ID, true); err == nil ||
|
|
!strings.Contains(err.Error(), "abwesend") {
|
|
t.Fatalf("accept error = %v, want absence rejection", err)
|
|
}
|
|
if assignmentPerson(t, f.st, f.tenantID, f.annaJob.ID) != "p-anna" {
|
|
t.Fatal("rejected acceptance still moved the offered assignment")
|
|
}
|
|
if assignmentPerson(t, f.st, f.tenantID, f.markJob.ID) != "p-markus" {
|
|
t.Fatal("rejected acceptance still moved the wanted assignment")
|
|
}
|
|
marks, err := f.st.PendingSwapAssignmentIDs(ctx, f.tenantID)
|
|
if err != nil {
|
|
t.Fatalf("pending marks: %v", err)
|
|
}
|
|
if marks[f.annaJob.ID] == "" || marks[f.markJob.ID] == "" {
|
|
t.Fatal("open request lost its plan marks after a rejected acceptance")
|
|
}
|
|
}
|
|
|
|
func TestRevokingConsentCancelsPendingSwap(t *testing.T) {
|
|
f := newReaderPairFixture(t)
|
|
mutualShares(t, f)
|
|
requestID, _ := createSwapRequest(t, f)
|
|
|
|
// Anna revokes the share; the pending request can no longer be answered
|
|
// and the plan markers must disappear.
|
|
shareContacts(t, f.anna, f.ts.URL)
|
|
|
|
request, err := f.st.SwapRequestByID(context.Background(), f.tenantID, requestID)
|
|
if err != nil {
|
|
t.Fatalf("load request: %v", err)
|
|
}
|
|
if request.Status != store.SwapCancelled {
|
|
t.Fatalf("request status = %q, want cancelled", request.Status)
|
|
}
|
|
marks, err := f.st.PendingSwapAssignmentIDs(context.Background(), f.tenantID)
|
|
if err != nil {
|
|
t.Fatalf("pending marks: %v", err)
|
|
}
|
|
if marks[f.annaJob.ID] != "" || marks[f.markJob.ID] != "" {
|
|
t.Fatal("cancelled swap is still marked as pending")
|
|
}
|
|
}
|
|
|
|
func TestSwapRequestRequiresMutualConsent(t *testing.T) {
|
|
f := newReaderPairFixture(t)
|
|
// Only the requester shares; the target has not shared the other way.
|
|
shareContacts(t, f.anna, f.ts.URL, f.markUser.ID)
|
|
|
|
get(t, f.anna, f.ts.URL+"/swap/new?assignment="+f.markJob.ID, http.StatusForbidden)
|
|
page := get(t, f.anna, f.ts.URL+"/services/"+f.markSvc.ID, http.StatusOK)
|
|
if strings.Contains(page, "/swap/new?assignment="+f.markJob.ID) {
|
|
t.Fatal("swap button is offered without mutual consent")
|
|
}
|
|
}
|
|
|
|
func TestSwapRejectsAbsence(t *testing.T) {
|
|
f := newReaderPairFixture(t)
|
|
mutualShares(t, f)
|
|
ctx := context.Background()
|
|
if err := f.st.UpsertAbsence(ctx, f.tenantID, domain.Absence{
|
|
ID: "swap-absence", PersonID: "p-markus",
|
|
Start: f.annaSvc.Date, End: f.annaSvc.Date, Reason: "Testabwesenheit",
|
|
}); err != nil {
|
|
t.Fatalf("create absence: %v", err)
|
|
}
|
|
|
|
_, _, err := f.st.CreateSwapRequest(ctx, f.tenantID, f.annaUser.ID,
|
|
"p-anna", f.markUser.ID, "p-markus", f.annaJob.ID, f.markJob.ID)
|
|
if err == nil || !strings.Contains(err.Error(), "abwesend") {
|
|
t.Fatalf("swap request error = %v, want absence rejection", err)
|
|
}
|
|
options, err := f.st.SwapOptions(ctx, f.tenantID, "p-anna", "p-markus", f.markJob.ID)
|
|
if err != nil {
|
|
t.Fatalf("swap options: %v", err)
|
|
}
|
|
if len(options) != 0 {
|
|
t.Fatalf("swap options = %d, want none while the target is absent", len(options))
|
|
}
|
|
}
|
|
|
|
func personHasAssignment(t *testing.T, st *store.Store, tenantID, personID, serviceID string) bool {
|
|
t.Helper()
|
|
assignments, err := st.AssignmentsForPerson(context.Background(), tenantID, personID)
|
|
if err != nil {
|
|
t.Fatalf("assignments for %s: %v", personID, err)
|
|
}
|
|
for _, assignment := range assignments {
|
|
if assignment.ServiceID == serviceID {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func assignmentPerson(t *testing.T, st *store.Store, tenantID, assignmentID string) string {
|
|
t.Helper()
|
|
info, err := st.SwapAssignmentInfo(context.Background(), tenantID, assignmentID)
|
|
if err != nil {
|
|
t.Fatalf("assignment %s: %v", assignmentID, err)
|
|
}
|
|
return info.Assignment.PersonID
|
|
}
|