eleven / messenger Public

Clone
git clone https://amadan.net/eleven/messenger

Plain git — no account needed to clone.

Download

Download this file

1package main
2
3import (
4 "encoding/json"
5 "net/http"
6 "testing"
7)
8
9// The public price book (#1374): CORS-open pass-through of the provisioner's
10// tiers, cached, empty when no provisioner is configured.
11func TestPricebookServesTiersCORSOpenAndCached(t *testing.T) {
12 tiers := []map[string]any{{"key": "family", "name": "Family", "label": "€5.50 a year"}}
13 cloud, _, _ := fakeCloud(t, http.StatusOK, map[string]any{
14 "billing": map[string]any{"tiers": tiers},
15 })
16 ts, s := newTestServer(t)
17 s.cloud = newCloudProvisioner(cloud.URL, "caller-token")
18
19 resp, err := http.Get(ts.URL + "/api/pricebook")
20 if err != nil {
21 t.Fatal(err)
22 }
23 defer resp.Body.Close()
24 if resp.StatusCode != http.StatusOK {
25 t.Fatalf("status %d", resp.StatusCode)
26 }
27 // The caller is a SPACE origin's browser JS — the endpoint must be open.
28 if got := resp.Header.Get("Access-Control-Allow-Origin"); got != "*" {
29 t.Errorf("Access-Control-Allow-Origin = %q, want *", got)
30 }
31 var body struct {
32 Tiers []map[string]any `json:"tiers"`
33 }
34 if err := json.NewDecoder(resp.Body).Decode(&body); err != nil {
35 t.Fatal(err)
36 }
37 if len(body.Tiers) != 1 || body.Tiers[0]["key"] != "family" {
38 t.Fatalf("tiers = %v, want the fake cloud's family tier", body.Tiers)
39 }
40
41 // Cached: kill the upstream; the answer must not change within the TTL.
42 cloud.Close()
43 resp2, err := http.Get(ts.URL + "/api/pricebook")
44 if err != nil {
45 t.Fatal(err)
46 }
47 defer resp2.Body.Close()
48 var body2 struct {
49 Tiers []map[string]any `json:"tiers"`
50 }
51 if err := json.NewDecoder(resp2.Body).Decode(&body2); err != nil {
52 t.Fatal(err)
53 }
54 if len(body2.Tiers) != 1 {
55 t.Fatalf("cached tiers = %v, want the earlier copy", body2.Tiers)
56 }
57}
58
59func TestPricebookEmptyWithoutProvisioner(t *testing.T) {
60 ts, _ := newTestServer(t)
61 resp, err := http.Get(ts.URL + "/api/pricebook")
62 if err != nil {
63 t.Fatal(err)
64 }
65 defer resp.Body.Close()
66 if resp.StatusCode != http.StatusOK {
67 t.Fatalf("status %d", resp.StatusCode)
68 }
69 var body struct {
70 Tiers []map[string]any `json:"tiers"`
71 }
72 if err := json.NewDecoder(resp.Body).Decode(&body); err != nil {
73 t.Fatal(err)
74 }
75 if len(body.Tiers) != 0 {
76 t.Fatalf("tiers = %v, want empty (no provisioner)", body.Tiers)
77 }
78}
79