|
ce03bda…
|
lmata
|
1 |
package client_test |
|
ce03bda…
|
lmata
|
2 |
|
|
ce03bda…
|
lmata
|
3 |
import ( |
|
ce03bda…
|
lmata
|
4 |
"context" |
|
ce03bda…
|
lmata
|
5 |
"encoding/json" |
|
ce03bda…
|
lmata
|
6 |
"net/http" |
|
ce03bda…
|
lmata
|
7 |
"net/http/httptest" |
|
ce03bda…
|
lmata
|
8 |
"testing" |
|
ce03bda…
|
lmata
|
9 |
|
|
ce03bda…
|
lmata
|
10 |
"github.com/conflicthq/scuttlebot/pkg/client" |
|
ce03bda…
|
lmata
|
11 |
) |
|
ce03bda…
|
lmata
|
12 |
|
|
ce03bda…
|
lmata
|
13 |
func TestTopologyClientCreateChannel(t *testing.T) { |
|
ce03bda…
|
lmata
|
14 |
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
|
ce03bda…
|
lmata
|
15 |
if r.Method != http.MethodPost || r.URL.Path != "/v1/channels" { |
|
ce03bda…
|
lmata
|
16 |
http.Error(w, "unexpected", http.StatusBadRequest) |
|
ce03bda…
|
lmata
|
17 |
return |
|
ce03bda…
|
lmata
|
18 |
} |
|
ce03bda…
|
lmata
|
19 |
if r.Header.Get("Authorization") != "Bearer tok" { |
|
ce03bda…
|
lmata
|
20 |
http.Error(w, "unauthorized", http.StatusUnauthorized) |
|
ce03bda…
|
lmata
|
21 |
return |
|
ce03bda…
|
lmata
|
22 |
} |
|
ce03bda…
|
lmata
|
23 |
var req struct { |
|
ce03bda…
|
lmata
|
24 |
Name string `json:"name"` |
|
ce03bda…
|
lmata
|
25 |
Topic string `json:"topic"` |
|
ce03bda…
|
lmata
|
26 |
} |
|
ce03bda…
|
lmata
|
27 |
if err := json.NewDecoder(r.Body).Decode(&req); err != nil { |
|
ce03bda…
|
lmata
|
28 |
http.Error(w, err.Error(), http.StatusBadRequest) |
|
ce03bda…
|
lmata
|
29 |
return |
|
ce03bda…
|
lmata
|
30 |
} |
|
ce03bda…
|
lmata
|
31 |
w.Header().Set("Content-Type", "application/json") |
|
ce03bda…
|
lmata
|
32 |
w.WriteHeader(http.StatusCreated) |
|
ce03bda…
|
lmata
|
33 |
_ = json.NewEncoder(w).Encode(map[string]any{ |
|
ce03bda…
|
lmata
|
34 |
"channel": req.Name, |
|
ce03bda…
|
lmata
|
35 |
"type": "task", |
|
ce03bda…
|
lmata
|
36 |
"supervision": "#general", |
|
ce03bda…
|
lmata
|
37 |
"autojoin": []string{"bridge", "scribe"}, |
|
ce03bda…
|
lmata
|
38 |
}) |
|
ce03bda…
|
lmata
|
39 |
})) |
|
ce03bda…
|
lmata
|
40 |
defer srv.Close() |
|
ce03bda…
|
lmata
|
41 |
|
|
ce03bda…
|
lmata
|
42 |
tc := client.NewTopologyClient(srv.URL, "tok") |
|
ce03bda…
|
lmata
|
43 |
info, err := tc.CreateChannel(context.Background(), "#task.gh-42", "GitHub issue #42") |
|
ce03bda…
|
lmata
|
44 |
if err != nil { |
|
ce03bda…
|
lmata
|
45 |
t.Fatal(err) |
|
ce03bda…
|
lmata
|
46 |
} |
|
ce03bda…
|
lmata
|
47 |
if info.Channel != "#task.gh-42" { |
|
ce03bda…
|
lmata
|
48 |
t.Errorf("Channel = %q, want #task.gh-42", info.Channel) |
|
ce03bda…
|
lmata
|
49 |
} |
|
ce03bda…
|
lmata
|
50 |
if info.Type != "task" { |
|
ce03bda…
|
lmata
|
51 |
t.Errorf("Type = %q, want task", info.Type) |
|
ce03bda…
|
lmata
|
52 |
} |
|
ce03bda…
|
lmata
|
53 |
if info.Supervision != "#general" { |
|
ce03bda…
|
lmata
|
54 |
t.Errorf("Supervision = %q, want #general", info.Supervision) |
|
ce03bda…
|
lmata
|
55 |
} |
|
ce03bda…
|
lmata
|
56 |
if len(info.Autojoin) != 2 { |
|
ce03bda…
|
lmata
|
57 |
t.Errorf("Autojoin = %v, want 2 entries", info.Autojoin) |
|
ce03bda…
|
lmata
|
58 |
} |
|
ce03bda…
|
lmata
|
59 |
} |
|
ce03bda…
|
lmata
|
60 |
|
|
ce03bda…
|
lmata
|
61 |
func TestTopologyClientGetTopology(t *testing.T) { |
|
ce03bda…
|
lmata
|
62 |
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
|
ce03bda…
|
lmata
|
63 |
if r.URL.Path != "/v1/topology" { |
|
ce03bda…
|
lmata
|
64 |
http.Error(w, "not found", http.StatusNotFound) |
|
ce03bda…
|
lmata
|
65 |
return |
|
ce03bda…
|
lmata
|
66 |
} |
|
ce03bda…
|
lmata
|
67 |
w.Header().Set("Content-Type", "application/json") |
|
ce03bda…
|
lmata
|
68 |
_ = json.NewEncoder(w).Encode(map[string]any{ |
|
ce03bda…
|
lmata
|
69 |
"static_channels": []string{"#general", "#alerts"}, |
|
ce03bda…
|
lmata
|
70 |
"types": []map[string]any{ |
|
ce03bda…
|
lmata
|
71 |
{"name": "task", "prefix": "task.", "autojoin": []string{"bridge"}}, |
|
ce03bda…
|
lmata
|
72 |
}, |
|
ce03bda…
|
lmata
|
73 |
}) |
|
ce03bda…
|
lmata
|
74 |
})) |
|
ce03bda…
|
lmata
|
75 |
defer srv.Close() |
|
ce03bda…
|
lmata
|
76 |
|
|
ce03bda…
|
lmata
|
77 |
tc := client.NewTopologyClient(srv.URL, "tok") |
|
ce03bda…
|
lmata
|
78 |
statics, types, err := tc.GetTopology(context.Background()) |
|
ce03bda…
|
lmata
|
79 |
if err != nil { |
|
ce03bda…
|
lmata
|
80 |
t.Fatal(err) |
|
ce03bda…
|
lmata
|
81 |
} |
|
ce03bda…
|
lmata
|
82 |
if len(statics) != 2 || statics[0] != "#general" { |
|
ce03bda…
|
lmata
|
83 |
t.Errorf("static_channels = %v", statics) |
|
ce03bda…
|
lmata
|
84 |
} |
|
ce03bda…
|
lmata
|
85 |
if len(types) != 1 || types[0].Name != "task" { |
|
ce03bda…
|
lmata
|
86 |
t.Errorf("types = %v", types) |
|
ce03bda…
|
lmata
|
87 |
} |
|
ce03bda…
|
lmata
|
88 |
} |
|
ce03bda…
|
lmata
|
89 |
|
|
ce03bda…
|
lmata
|
90 |
func TestTopologyClientDropChannel(t *testing.T) { |
|
ce03bda…
|
lmata
|
91 |
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
|
ce03bda…
|
lmata
|
92 |
if r.Method != http.MethodDelete { |
|
ce03bda…
|
lmata
|
93 |
http.Error(w, "wrong method", http.StatusMethodNotAllowed) |
|
ce03bda…
|
lmata
|
94 |
return |
|
ce03bda…
|
lmata
|
95 |
} |
|
ce03bda…
|
lmata
|
96 |
if r.URL.Path != "/v1/topology/channels/task.gh-42" { |
|
ce03bda…
|
lmata
|
97 |
http.Error(w, "wrong path: "+r.URL.Path, http.StatusBadRequest) |
|
ce03bda…
|
lmata
|
98 |
return |
|
ce03bda…
|
lmata
|
99 |
} |
|
ce03bda…
|
lmata
|
100 |
w.WriteHeader(http.StatusNoContent) |
|
ce03bda…
|
lmata
|
101 |
})) |
|
ce03bda…
|
lmata
|
102 |
defer srv.Close() |
|
ce03bda…
|
lmata
|
103 |
|
|
ce03bda…
|
lmata
|
104 |
tc := client.NewTopologyClient(srv.URL, "tok") |
|
ce03bda…
|
lmata
|
105 |
if err := tc.DropChannel(context.Background(), "#task.gh-42"); err != nil { |
|
ce03bda…
|
lmata
|
106 |
t.Fatal(err) |
|
ce03bda…
|
lmata
|
107 |
} |
|
ce03bda…
|
lmata
|
108 |
} |