package web_test import ( "context" "net/http" "net/url" "strings" "testing" ) func TestEinladungFormZeigtFirmenname(t *testing.T) { fs := newFakeStore() s := newServer(t, fs) acc, err := fs.CreateAccount(context.Background(), "Beispiel GmbH") if err != nil { t.Fatalf("CreateAccount: %v", err) } resp := getWithCookie(t, s, nil, "/einladung/"+acc.EinladungToken) if resp.Code != http.StatusOK { t.Fatalf("status = %d, body: %s", resp.Code, resp.Body.String()) } if !strings.Contains(resp.Body.String(), "Beispiel GmbH") { t.Errorf("expected the Firmenname on the Einladung page, got: %s", resp.Body.String()) } } func TestEinladungMitUnbekanntemTokenZeigtFehler(t *testing.T) { fs := newFakeStore() s := newServer(t, fs) resp := getWithCookie(t, s, nil, "/einladung/unbekanntes-token") if resp.Code != http.StatusOK { t.Fatalf("status = %d, body: %s", resp.Code, resp.Body.String()) } if !strings.Contains(resp.Body.String(), "ungültig") { t.Errorf("expected an invalid-link message, got: %s", resp.Body.String()) } } func TestEinladungAnnehmenLegtMitarbeiterAn(t *testing.T) { fs := newFakeStore() s := newServer(t, fs) acc, err := fs.CreateAccount(context.Background(), "Beispiel GmbH") if err != nil { t.Fatalf("CreateAccount: %v", err) } resp := postForm(t, s, nil, "/einladung/"+acc.EinladungToken, url.Values{ "email": {"neu@example.com"}, "password": {"ein-langes-passwort"}, }) if resp.Code != http.StatusSeeOther { t.Fatalf("status = %d, want 303, body: %s", resp.Code, resp.Body.String()) } if len(resp.Result().Cookies()) == 0 { t.Fatal("expected a session cookie to be set") } user, err := fs.GetUserByEmail(context.Background(), "neu@example.com") if err != nil { t.Fatalf("GetUserByEmail: %v", err) } if user.Role != "mitarbeiter" || user.AccountID != acc.ID { t.Errorf("User = %+v, want role mitarbeiter in account %s", user, acc.ID) } cookie := resp.Result().Cookies()[0] protected := getWithCookie(t, s, cookie, "/antraege") if protected.Code != http.StatusOK { t.Fatalf("expected the new session to work, status = %d", protected.Code) } } func TestEinladungAnnehmenMitUnbekanntemTokenSchlaegtFehl(t *testing.T) { fs := newFakeStore() s := newServer(t, fs) resp := postForm(t, s, nil, "/einladung/unbekanntes-token", url.Values{ "email": {"neu@example.com"}, "password": {"ein-langes-passwort"}, }) if resp.Code != http.StatusOK { t.Fatalf("status = %d, body: %s", resp.Code, resp.Body.String()) } if !strings.Contains(resp.Body.String(), "ungültig") { t.Errorf("expected an invalid-link message, got: %s", resp.Body.String()) } if len(fs.users) != 0 { t.Error("expected no user to be created for an invalid token") } } func TestAdminSiehtEinladungslink(t *testing.T) { fs := newFakeStore() s := newServer(t, fs) adminCookie := seedAccountWithRole(t, fs, "Test-Mandant", "admin@example.com", "admin") resp := getWithCookie(t, s, adminCookie, "/verwaltung/einladung") if resp.Code != http.StatusOK { t.Fatalf("status = %d, body: %s", resp.Code, resp.Body.String()) } if !strings.Contains(resp.Body.String(), "/einladung/") { t.Errorf("expected the Einladungslink on the page, got: %s", resp.Body.String()) } } func TestAdminKannEinladungslinkErneuern(t *testing.T) { fs := newFakeStore() s := newServer(t, fs) adminCookie := seedAccountWithRole(t, fs, "Test-Mandant", "admin@example.com", "admin") admin, err := fs.GetUserByEmail(context.Background(), "admin@example.com") if err != nil { t.Fatalf("GetUserByEmail: %v", err) } altesToken := fs.accounts[admin.AccountID].EinladungToken resp := postForm(t, s, adminCookie, "/verwaltung/einladung/erneuern", url.Values{}) if resp.Code != http.StatusSeeOther { t.Fatalf("status = %d, body: %s", resp.Code, resp.Body.String()) } neuesToken := fs.accounts[admin.AccountID].EinladungToken if neuesToken == altesToken { t.Fatal("expected the token to change") } oldLinkResp := getWithCookie(t, s, nil, "/einladung/"+altesToken) if !strings.Contains(oldLinkResp.Body.String(), "ungültig") { t.Errorf("expected the old link to be invalid, got: %s", oldLinkResp.Body.String()) } newLinkResp := getWithCookie(t, s, nil, "/einladung/"+neuesToken) if newLinkResp.Code != http.StatusOK || strings.Contains(newLinkResp.Body.String(), "ungültig") { t.Errorf("expected the new link to work, status=%d body: %s", newLinkResp.Code, newLinkResp.Body.String()) } } func TestMitarbeiterCannotAccessEinladungsverwaltung(t *testing.T) { fs := newFakeStore() s := newServer(t, fs) cookie := seedAccountWithRole(t, fs, "Test-Mandant", "mitarbeiter@example.com", "mitarbeiter") resp := getWithCookie(t, s, cookie, "/verwaltung/einladung") if resp.Code != http.StatusNotFound { t.Fatalf("status = %d, want 404 for role mitarbeiter", resp.Code) } }