github.com/diamondburned/arikawa@v1.3.14/api/send_test.go (about) 1 package api 2 3 import ( 4 "encoding/json" 5 "strings" 6 "testing" 7 8 "github.com/diamondburned/arikawa/discord" 9 ) 10 11 func TestMarshalAllowedMentions(t *testing.T) { 12 t.Run("parse nothing", func(t *testing.T) { 13 var data = SendMessageData{ 14 AllowedMentions: &AllowedMentions{ 15 Parse: []AllowedMentionType{}, 16 }, 17 } 18 19 if j := mustMarshal(t, data); j != `{"allowed_mentions":{"parse":[]}}` { 20 t.Fatal("Unexpected JSON:", j) 21 } 22 }) 23 24 t.Run("allow everything", func(t *testing.T) { 25 var data = SendMessageData{ 26 Content: "a", 27 } 28 29 if j := mustMarshal(t, data); j != `{"content":"a"}` { 30 t.Fatal("Unexpected JSON:", j) 31 } 32 }) 33 34 t.Run("allow certain user IDs", func(t *testing.T) { 35 var data = SendMessageData{ 36 AllowedMentions: &AllowedMentions{ 37 Users: []discord.UserID{1, 2}, 38 }, 39 } 40 41 if j := mustMarshal(t, data); j != `{"allowed_mentions":{"parse":null,"users":["1","2"]}}` { 42 t.Fatal("Unexpected JSON:", j) 43 } 44 }) 45 } 46 47 func TestVerifyAllowedMentions(t *testing.T) { 48 t.Run("invalid", func(t *testing.T) { 49 var am = AllowedMentions{ 50 Parse: []AllowedMentionType{AllowEveryoneMention, AllowUserMention}, 51 Users: []discord.UserID{69, 420}, 52 } 53 54 err := am.Verify() 55 errMustContain(t, err, "Users slice is not empty") 56 }) 57 58 t.Run("users too long", func(t *testing.T) { 59 var am = AllowedMentions{ 60 Users: make([]discord.UserID, 101), 61 } 62 63 err := am.Verify() 64 errMustContain(t, err, "users slice length 101 is over 100") 65 }) 66 67 t.Run("roles too long", func(t *testing.T) { 68 var am = AllowedMentions{ 69 Roles: make([]discord.RoleID, 101), 70 } 71 72 err := am.Verify() 73 errMustContain(t, err, "roles slice length 101 is over 100") 74 }) 75 76 t.Run("valid", func(t *testing.T) { 77 var am = AllowedMentions{ 78 Parse: []AllowedMentionType{AllowEveryoneMention, AllowUserMention}, 79 Roles: []discord.RoleID{1337}, 80 Users: []discord.UserID{}, 81 } 82 83 if err := am.Verify(); err != nil { 84 t.Fatal("Unexpected error:", err) 85 } 86 }) 87 } 88 89 func TestSendMessage(t *testing.T) { 90 send := func(data SendMessageData) error { 91 // A nil client will cause a panic. 92 defer func() { 93 recover() 94 }() 95 96 // shouldn't matter 97 client := (*Client)(nil) 98 _, err := client.SendMessageComplex(0, data) 99 return err 100 } 101 102 t.Run("empty", func(t *testing.T) { 103 var empty = SendMessageData{ 104 Content: "", 105 Embed: nil, 106 } 107 108 if err := send(empty); err != ErrEmptyMessage { 109 t.Fatal("Unexpected error:", err) 110 } 111 }) 112 113 t.Run("files only", func(t *testing.T) { 114 var empty = SendMessageData{ 115 Files: []SendMessageFile{{Name: "test.jpg"}}, 116 } 117 118 if err := send(empty); err != nil { 119 t.Fatal("Unexpected error:", err) 120 } 121 }) 122 123 t.Run("invalid allowed mentions", func(t *testing.T) { 124 var data = SendMessageData{ 125 Content: "hime arikawa", 126 AllowedMentions: &AllowedMentions{ 127 Parse: []AllowedMentionType{AllowEveryoneMention, AllowUserMention}, 128 Users: []discord.UserID{69, 420}, 129 }, 130 } 131 132 err := send(data) 133 errMustContain(t, err, "allowedMentions error") 134 }) 135 136 t.Run("invalid embed", func(t *testing.T) { 137 var data = SendMessageData{ 138 Embed: &discord.Embed{ 139 // max 256 140 Title: spaces(257), 141 }, 142 } 143 144 err := send(data) 145 errMustContain(t, err, "embed error") 146 }) 147 } 148 149 func errMustContain(t *testing.T, err error, contains string) { 150 // mark function as helper so line traces are accurate. 151 t.Helper() 152 153 if err != nil && strings.Contains(err.Error(), contains) { 154 return 155 } 156 t.Fatal("Unexpected error:", err) 157 } 158 159 func spaces(length int) string { 160 return strings.Repeat(" ", length) 161 } 162 163 func mustMarshal(t *testing.T, v interface{}) string { 164 t.Helper() 165 166 j, err := json.Marshal(v) 167 if err != nil { 168 t.Fatal("Failed to marshal data:", err) 169 } 170 return string(j) 171 }