|
| 1 | +package slackrus |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/johntdyer/slack-go" |
| 10 | + "github.com/sirupsen/logrus" |
| 11 | +) |
| 12 | + |
| 13 | +type Fixture struct { |
| 14 | + Messages []slack.Message |
| 15 | + Cleanup func() |
| 16 | + MsgRcvd chan struct{} |
| 17 | + |
| 18 | + server *httptest.Server |
| 19 | +} |
| 20 | + |
| 21 | +func (f *Fixture) URL() string { |
| 22 | + return f.server.URL |
| 23 | +} |
| 24 | + |
| 25 | +func NewFixture(t *testing.T) *Fixture { |
| 26 | + f := &Fixture{MsgRcvd: make(chan struct{}, 1)} |
| 27 | + f.server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 28 | + var msg slack.Message |
| 29 | + dec := json.NewDecoder(r.Body) |
| 30 | + err := dec.Decode(&msg) |
| 31 | + if err != nil { |
| 32 | + w.WriteHeader(500) |
| 33 | + return |
| 34 | + } |
| 35 | + f.Messages = append(f.Messages, msg) |
| 36 | + select { |
| 37 | + case f.MsgRcvd <- struct{}{}: |
| 38 | + default: |
| 39 | + } |
| 40 | + })) |
| 41 | + f.Cleanup = func() { f.server.Close() } |
| 42 | + return f |
| 43 | +} |
| 44 | + |
| 45 | +func TestFieldSorting(t *testing.T) { |
| 46 | + f := NewFixture(t) |
| 47 | + defer f.Cleanup() |
| 48 | + |
| 49 | + logger := logrus.New() |
| 50 | + logger.AddHook(&SlackrusHook{ |
| 51 | + HookURL: f.URL(), |
| 52 | + Channel: "#slack-testing", |
| 53 | + SortFields: true, |
| 54 | + }) |
| 55 | + |
| 56 | + first, second, third := "a_should_be_first", "b_should_be_second", "c_should_be_third" |
| 57 | + |
| 58 | + logger.WithFields(logrus.Fields{ |
| 59 | + second: "b content", |
| 60 | + third: "c content", |
| 61 | + first: "a content", |
| 62 | + }).Info("well hello there, you better sort my fields!!!!") |
| 63 | + |
| 64 | + <-f.MsgRcvd |
| 65 | + |
| 66 | + if exp, got := 1, len(f.Messages); exp != got { |
| 67 | + t.Fatalf("received unexpected number of messages: exp: %d, got: %d", exp, got) |
| 68 | + } |
| 69 | + msg := f.Messages[0] |
| 70 | + if exp, got := 1, len(msg.Attachments); exp != got { |
| 71 | + t.Fatalf("received unexpected number of Attachments in message: exp: %d, got: %d", exp, got) |
| 72 | + } |
| 73 | + fields := msg.Attachments[0].Fields |
| 74 | + if exp, got := 3, len(fields); exp != got { |
| 75 | + t.Fatalf("received unexpected number of Fields in attachment: exp: %d, got: %d", exp, got) |
| 76 | + } |
| 77 | + |
| 78 | + if exp, got := first, fields[0].Title; exp != got { |
| 79 | + t.Errorf("0-th field title not as expected: exp: %q, got: %q", exp, got) |
| 80 | + } |
| 81 | + if exp, got := second, fields[1].Title; exp != got { |
| 82 | + t.Errorf("1st field title not as expected: exp: %q, got: %q", exp, got) |
| 83 | + } |
| 84 | + if exp, got := third, fields[2].Title; exp != got { |
| 85 | + t.Errorf("2nd field title not as expected: exp: %q, got: %q", exp, got) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func TestFieldSortPriorities(t *testing.T) { |
| 90 | + f := NewFixture(t) |
| 91 | + defer f.Cleanup() |
| 92 | + |
| 93 | + first, second, third, fourth := "d_should_be_first", "b_should_be_second", "a_should_be_third", "c_should_be_fourth" |
| 94 | + |
| 95 | + logger := logrus.New() |
| 96 | + logger.AddHook(&SlackrusHook{ |
| 97 | + HookURL: f.URL(), |
| 98 | + Channel: "#slack-testing", |
| 99 | + SortFields: true, |
| 100 | + SortPriorities: map[string]int{ |
| 101 | + first: 10, |
| 102 | + second: 5, |
| 103 | + }, |
| 104 | + }) |
| 105 | + |
| 106 | + logger.WithFields(logrus.Fields{ |
| 107 | + first: "first content", |
| 108 | + second: "second content", |
| 109 | + third: "third content", |
| 110 | + fourth: "fourth content", |
| 111 | + }).Info("well hello there, you better sort my fields!!!!") |
| 112 | + |
| 113 | + <-f.MsgRcvd |
| 114 | + |
| 115 | + if exp, got := 1, len(f.Messages); exp != got { |
| 116 | + t.Fatalf("received unexpected number of messages: exp: %d, got: %d", exp, got) |
| 117 | + } |
| 118 | + msg := f.Messages[0] |
| 119 | + if exp, got := 1, len(msg.Attachments); exp != got { |
| 120 | + t.Fatalf("received unexpected number of Attachments in message: exp: %d, got: %d", exp, got) |
| 121 | + } |
| 122 | + fields := msg.Attachments[0].Fields |
| 123 | + if exp, got := 4, len(fields); exp != got { |
| 124 | + t.Fatalf("received unexpected number of Fields in attachment: exp: %d, got: %d", exp, got) |
| 125 | + } |
| 126 | + |
| 127 | + if exp, got := first, fields[0].Title; exp != got { |
| 128 | + t.Errorf("0-th field title not as expected: exp: %q, got: %q", exp, got) |
| 129 | + } |
| 130 | + if exp, got := second, fields[1].Title; exp != got { |
| 131 | + t.Errorf("1st field title not as expected: exp: %q, got: %q", exp, got) |
| 132 | + } |
| 133 | + if exp, got := third, fields[2].Title; exp != got { |
| 134 | + t.Errorf("2nd field title not as expected: exp: %q, got: %q", exp, got) |
| 135 | + } |
| 136 | + if exp, got := fourth, fields[3].Title; exp != got { |
| 137 | + t.Errorf("3rd field title not as expected: exp: %q, got: %q", exp, got) |
| 138 | + } |
| 139 | +} |
0 commit comments