package governance import ( "errors" "testing" "time" ) // TestLinkCodeLinksOnlyOnceToItsIssuer guards linking a channel account: a // code works once, before it expires, for the connector it was issued for and // for an active account, and a channel account that another account already // holds is never moved to the issuer. func TestLinkCodeLinksOnlyOnceToItsIssuer(t *testing.T) { now := time.Date(2026, 11, 22, 10, 0, 0, 0, time.UTC) fresh := linkCode{account: "alice", issuer: "telegram", expiresAt: now.Add(time.Minute), active: true} tests := []struct { name string code linkCode issuer string linkedTo string want error }{ {name: "a fresh code links", code: fresh, issuer: "telegram"}, {name: "a channel account already linked to the issuer links again", code: fresh, issuer: "telegram", linkedTo: "alice"}, {name: "a used code is refused", code: func() linkCode { c := fresh; c.used = true; return c }(), issuer: "telegram", want: ErrLinkCode}, {name: "an expired code is refused", code: func() linkCode { c := fresh; c.expiresAt = now; return c }(), issuer: "telegram", want: ErrLinkCode}, {name: "a code for another connector is refused", code: fresh, issuer: "slack", want: ErrLinkCode}, {name: "a suspended account's code is refused", code: func() linkCode { c := fresh; c.active = false; return c }(), issuer: "telegram", want: ErrLinkCode}, {name: "a channel account held by another account is refused", code: fresh, issuer: "telegram", linkedTo: "bob", want: ErrLinkTaken}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := tt.code.redeem(tt.issuer, tt.linkedTo, now); !errors.Is(err, tt.want) { t.Fatalf("redeem = %v, want %v", err, tt.want) } }) } }