ScuttleBot

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

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button