| 1 | package coordinatormail |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | func scope() Scope { |
| 6 | return Scope{"team", "person", "person@example.com", "coordinator", "project", "task", "thread"} |
| 7 | } |
| 8 | |
| 9 | func TestOutbound(t *testing.T) { |
| 10 | good := Outbound{ |
| 11 | Scope: scope(), Recipient: "person@example.com", |
| 12 | Sender: "coordinator@example.com", SenderLabel: "Equipo coordinator", |
| 13 | Configured: true, Authorized: true, Verified: true, Active: true, OptedIn: true, |
| 14 | } |
| 15 | if err := CheckOutbound(good); err != nil { |
| 16 | t.Fatal(err) |
| 17 | } |
| 18 | for name, mutate := range map[string]func(*Outbound){ |
| 19 | "unconfigured": func(o *Outbound) { o.Configured = false }, |
| 20 | "unauthorized": func(o *Outbound) { o.Authorized = false }, |
| 21 | "unverified": func(o *Outbound) { o.Verified = false }, |
| 22 | "revoked": func(o *Outbound) { o.Active = false }, |
| 23 | "opt out": func(o *Outbound) { o.OptedIn = false }, |
| 24 | "bounce": func(o *Outbound) { o.Suppressed = true }, |
| 25 | "other recipient": func(o *Outbound) { o.Recipient = "other@example.com" }, |
| 26 | "multiple recipients": func(o *Outbound) { o.Recipient += ",other@example.com" }, |
| 27 | "impersonation": func(o *Outbound) { o.Sender = o.Address }, |
| 28 | "undisclosed agent": func(o *Outbound) { o.SenderLabel = "Paul" }, |
| 29 | "header injection": func(o *Outbound) { o.Sender += "\r\nBcc: other@example.com" }, |
| 30 | "missing thread": func(o *Outbound) { o.Thread = "" }, |
| 31 | } { |
| 32 | t.Run(name, func(t *testing.T) { |
| 33 | o := good |
| 34 | mutate(&o) |
| 35 | if CheckOutbound(o) != ErrPolicy { |
| 36 | t.Fatal("accepted unsafe outbound") |
| 37 | } |
| 38 | }) |
| 39 | } |
| 40 | if CheckOutbound(Outbound{}) != ErrPolicy { |
| 41 | t.Fatal("zero value must fail closed") |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | func TestReply(t *testing.T) { |
| 46 | good := Reply{Expected: scope(), Bound: scope(), EventID: "event", |
| 47 | TransportVerified: true, SenderBound: true, RouteVerified: true, Active: true} |
| 48 | if err := CheckReply(good); err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | for name, mutate := range map[string]func(*Reply){ |
| 52 | "forged webhook": func(r *Reply) { r.TransportVerified = false }, |
| 53 | "From alone": func(r *Reply) { r.SenderBound = false }, |
| 54 | "subject alone": func(r *Reply) { r.RouteVerified = false }, |
| 55 | "revoked member": func(r *Reply) { r.Active = false }, |
| 56 | "auto reply": func(r *Reply) { r.Automated = true }, |
| 57 | "self loop": func(r *Reply) { r.FromCoordinator = true }, |
| 58 | "missing event": func(r *Reply) { r.EventID = "" }, |
| 59 | "other team": func(r *Reply) { r.Bound.Team = "other" }, |
| 60 | "other person": func(r *Reply) { r.Bound.Person = "other" }, |
| 61 | "other address": func(r *Reply) { r.Bound.Address = "other@example.com" }, |
| 62 | "other coordinator": func(r *Reply) { r.Bound.Coordinator = "other" }, |
| 63 | "other project": func(r *Reply) { r.Bound.Project = "other" }, |
| 64 | "other task": func(r *Reply) { r.Bound.Task = "other" }, |
| 65 | "other thread": func(r *Reply) { r.Bound.Thread = "other" }, |
| 66 | } { |
| 67 | t.Run(name, func(t *testing.T) { |
| 68 | r := good |
| 69 | mutate(&r) |
| 70 | if CheckReply(r) != ErrPolicy { |
| 71 | t.Fatal("accepted unsafe reply") |
| 72 | } |
| 73 | }) |
| 74 | } |
| 75 | if CheckReply(Reply{}) != ErrPolicy { |
| 76 | t.Fatal("zero value must fail closed") |
| 77 | } |
| 78 | } |
| 79 | |