Phase 13, part 3: signing up, and several teams per person
On the multi-tenant edition anyone can set up a team: their account, the company and its website, whose registered domain the team works on (the profile keeps to it). A person can belong to several teams and switch between them from the sidebar, or set up another. Inviting someone who has an account adds them to the team. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -3,6 +3,7 @@ package web
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -133,3 +134,117 @@ func newAgentIn(t *testing.T, ts *Server, name string, scopes map[string]users.A
|
||||
}
|
||||
return u, key
|
||||
}
|
||||
|
||||
func TestRegisteredDomain(t *testing.T) {
|
||||
for in, want := range map[string]string{"example.com": "example.com", "https://www.Example.com/x": "example.com",
|
||||
"shop.example.co.uk": "example.co.uk", "http://cloud.acme.io:8080": "acme.io"} {
|
||||
if got, err := registeredDomain(in); err != nil || got != want {
|
||||
t.Errorf("%s: %q %v", in, got, err)
|
||||
}
|
||||
}
|
||||
for _, bad := range []string{"", "localhost", "co.uk"} {
|
||||
if _, err := registeredDomain(bad); err == nil {
|
||||
t.Errorf("%q accepted", bad)
|
||||
}
|
||||
}
|
||||
if !onDomain("https://enterprise.acme.io", "acme.io") || onDomain("https://acme.io.evil.com", "acme.io") {
|
||||
t.Error("onDomain")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSignUpAndSwitchTeams(t *testing.T) {
|
||||
s, h := newMultiEdition(t)
|
||||
form := url.Values{"name": {"Olu"}, "email": {"olu@acme.example"}, "password": {"a long enough one"}, "company": {"Acme"}, "website": {"www.acme.io"}}
|
||||
if rec := do(h, http.MethodGet, "/signin", nil, nil); !strings.Contains(rec.Body.String(), `href="/signup"`) {
|
||||
t.Error("no sign-up link")
|
||||
}
|
||||
rec := postForm(h, "/signup", form)
|
||||
if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != "/settings/profile?msg=team-created" {
|
||||
t.Fatalf("sign up: %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
c := cookieOf(rec, sessionCookie)
|
||||
u, _ := users.ByEmail(s.platform(), "olu@acme.example")
|
||||
list, _ := users.TeamsOf(s.platform(), u.ID)
|
||||
if len(list) != 1 || list[0].Domain != "acme.io" || list[0].Name != "Acme" {
|
||||
t.Fatalf("teams %+v", list)
|
||||
}
|
||||
body := as(h, c, http.MethodGet, "/settings/profile", nil).Body.String()
|
||||
if !strings.Contains(body, "https://www.acme.io") {
|
||||
t.Error("profile not started")
|
||||
}
|
||||
// Same address again: sign in instead.
|
||||
if rec := postForm(h, "/signup", form); rec.Code != http.StatusConflict {
|
||||
t.Errorf("twice: %d", rec.Code)
|
||||
}
|
||||
// The profile keeps to the team's domain.
|
||||
rec = as(h, c, http.MethodPost, "/settings/profile", url.Values{"name": {"Acme"}, "description": {"x"}, "website_url": {"https://other.example"}})
|
||||
if !strings.Contains(rec.Body.String(), "isn't on acme.io") {
|
||||
t.Errorf("another domain accepted: %s", rec.Body.String())
|
||||
}
|
||||
|
||||
// A second team, from inside; the menu switches.
|
||||
rec = as(h, c, http.MethodPost, "/teams/new", url.Values{"company": {"Acme Labs"}, "website": {"acmelabs.dev"}})
|
||||
if rec.Code != http.StatusSeeOther {
|
||||
t.Fatalf("new team: %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
list, _ = users.TeamsOf(s.platform(), u.ID)
|
||||
if len(list) != 2 {
|
||||
t.Fatalf("teams %+v", list)
|
||||
}
|
||||
body = as(h, c, http.MethodGet, "/", nil).Body.String()
|
||||
if !strings.Contains(body, `<option value="`) || !strings.Contains(body, "Acme Labs") {
|
||||
t.Error("no team menu")
|
||||
}
|
||||
var acme, labs int64
|
||||
for _, tm := range list {
|
||||
if tm.Name == "Acme" {
|
||||
acme = tm.ID
|
||||
} else {
|
||||
labs = tm.ID
|
||||
}
|
||||
}
|
||||
if body := as(h, c, http.MethodGet, "/settings/profile", nil).Body.String(); !strings.Contains(body, "acmelabs.dev") {
|
||||
t.Error("not in the new team")
|
||||
}
|
||||
as(h, c, http.MethodPost, "/teams/switch", url.Values{"team": {strconv.FormatInt(acme, 10)}})
|
||||
if body := as(h, c, http.MethodGet, "/settings/profile", nil).Body.String(); !strings.Contains(body, "www.acme.io") {
|
||||
t.Error("didn't switch")
|
||||
}
|
||||
// Not someone else's team.
|
||||
other, _, _ := newTeam(t, s, "Bolt", "bea@bolt.example")
|
||||
if rec := as(h, c, http.MethodPost, "/teams/switch", url.Values{"team": {strconv.FormatInt(other.ID, 10)}}); rec.Code != http.StatusForbidden {
|
||||
t.Errorf("switched into another's team: %d", rec.Code)
|
||||
}
|
||||
_ = labs
|
||||
|
||||
// Bolt's owner invites Olu: Olu joins with the account he has.
|
||||
_, _, bc := newTeamOwnerCookie(t, s, other.ID)
|
||||
rec = as(h, bc, http.MethodPost, "/settings/users", url.Values{"email": {"olu@acme.example"}, "name": {"Olu"}, "role": {"member"}, "module:seo": {"view"}})
|
||||
if rec.Code != http.StatusSeeOther || !strings.Contains(rec.Header().Get("Location"), "added") {
|
||||
t.Fatalf("add existing: %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if list, _ = users.TeamsOf(s.platform(), u.ID); len(list) != 3 {
|
||||
t.Errorf("not added: %+v", list)
|
||||
}
|
||||
waitMail(t, s, "olu@acme.example", 1)
|
||||
// Signing in again opens the team used last.
|
||||
rec = signin(h, "203.0.113.50", url.Values{"email": {"olu@acme.example"}, "password": {"a long enough one"}})
|
||||
nc := cookieOf(rec, sessionCookie)
|
||||
if body := as(h, nc, http.MethodGet, "/settings/profile", nil).Body.String(); !strings.Contains(body, "www.acme.io") {
|
||||
t.Error("signed in to another team than the last")
|
||||
}
|
||||
}
|
||||
|
||||
// newTeamOwnerCookie signs in a team's owner.
|
||||
func newTeamOwnerCookie(t *testing.T, s *Server, team int64) (*users.User, int64, *http.Cookie) {
|
||||
t.Helper()
|
||||
list, _ := users.Members(s.platform(), team)
|
||||
for _, m := range list {
|
||||
if m.Role == users.Owner {
|
||||
token, _, _ := users.StartSessionIn(s.platform(), m.ID, team, "", "", time.Now())
|
||||
return m.User, team, &http.Cookie{Name: sessionCookie, Value: token}
|
||||
}
|
||||
}
|
||||
t.Fatal("no owner")
|
||||
return nil, 0, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user