|
| 1 | +package gapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/gobs/pretty" |
| 7 | +) |
| 8 | + |
| 9 | +func TestMessageTemplates(t *testing.T) { |
| 10 | + t.Run("get message templates succeeds", func(t *testing.T) { |
| 11 | + server, client := gapiTestTools(t, 200, getMessageTemplatesJSON) |
| 12 | + defer server.Close() |
| 13 | + |
| 14 | + ts, err := client.MessageTemplates() |
| 15 | + |
| 16 | + if err != nil { |
| 17 | + t.Error(err) |
| 18 | + } |
| 19 | + t.Log(pretty.PrettyFormat(ts)) |
| 20 | + if len(ts) != 2 { |
| 21 | + t.Errorf("wrong number of templates returned, got %#v", ts) |
| 22 | + } |
| 23 | + if ts[0].Name != "template-one" { |
| 24 | + t.Errorf("incorrect name - expected %s on element %d, got %#v", "template-one", 0, ts) |
| 25 | + } |
| 26 | + if ts[1].Name != "template-two" { |
| 27 | + t.Errorf("incorrect name - expected %s on element %d, got %#v", "template-two", 0, ts) |
| 28 | + } |
| 29 | + }) |
| 30 | + |
| 31 | + t.Run("get message template succeeds", func(t *testing.T) { |
| 32 | + server, client := gapiTestTools(t, 200, messageTemplateJSON) |
| 33 | + defer server.Close() |
| 34 | + |
| 35 | + tmpl, err := client.MessageTemplate("template-one") |
| 36 | + |
| 37 | + if err != nil { |
| 38 | + t.Error(err) |
| 39 | + } |
| 40 | + t.Log(pretty.PrettyFormat(tmpl)) |
| 41 | + if tmpl.Name != "template-one" { |
| 42 | + t.Errorf("incorrect name - expected %s, got %#v", "template-one", tmpl) |
| 43 | + } |
| 44 | + }) |
| 45 | + |
| 46 | + t.Run("get non-existent message template fails", func(t *testing.T) { |
| 47 | + server, client := gapiTestTools(t, 404, ``) |
| 48 | + defer server.Close() |
| 49 | + |
| 50 | + tmpl, err := client.MessageTemplate("does not exist") |
| 51 | + |
| 52 | + if err == nil { |
| 53 | + t.Errorf("expected error but got nil") |
| 54 | + t.Log(pretty.PrettyFormat(tmpl)) |
| 55 | + } |
| 56 | + }) |
| 57 | + |
| 58 | + t.Run("put message template succeeds", func(t *testing.T) { |
| 59 | + server, client := gapiTestTools(t, 202, messageTemplateJSON) |
| 60 | + defer server.Close() |
| 61 | + |
| 62 | + err := client.SetMessageTemplate("template-three", "{{define \"template-one\" }}\n content three\n{{ end }}") |
| 63 | + |
| 64 | + if err != nil { |
| 65 | + t.Error(err) |
| 66 | + } |
| 67 | + }) |
| 68 | + |
| 69 | + t.Run("delete message template succeeds", func(t *testing.T) { |
| 70 | + server, client := gapiTestTools(t, 204, ``) |
| 71 | + defer server.Close() |
| 72 | + |
| 73 | + err := client.DeleteMessageTemplate("template-three") |
| 74 | + |
| 75 | + if err != nil { |
| 76 | + t.Error(err) |
| 77 | + } |
| 78 | + }) |
| 79 | +} |
| 80 | + |
| 81 | +const getMessageTemplatesJSON = ` |
| 82 | +[ |
| 83 | + { |
| 84 | + "name": "template-one", |
| 85 | + "template": "{{define \"template-one\" }}\n content one\n{{ end }}" |
| 86 | + }, |
| 87 | + { |
| 88 | + "name": "template-two", |
| 89 | + "template": "{{define \"template-one\" }}\n content two\n{{ end }}" |
| 90 | + } |
| 91 | +] |
| 92 | +` |
| 93 | + |
| 94 | +const messageTemplateJSON = ` |
| 95 | +{ |
| 96 | + "name": "template-one", |
| 97 | + "template": "{{define \"template-one\" }}\n content one\n{{ end }}" |
| 98 | +} |
| 99 | +` |
0 commit comments