GOWA - WhatsApp REST API with support for UI, Multi Account, Webhooks, and MCP, and Chatwoot. Built with Golang for efficient memory use.
Go botgogolanggolang-whatsappgolang-whatsapp-apirestrest-apiwhatsappwhatsapp-apiwhatsapp-api-gowhatsapp-multi-devicewhatsapp-web-multi-device
package validations
import (
"context"
"testing"
domainMessage "github.com/aldinokemal/go-whatsapp-web-multidevice/domains/message"
pkgError "github.com/aldinokemal/go-whatsapp-web-multidevice/pkg/error"
"github.com/stretchr/testify/assert"
)
func TestValidateMarkAsRead(t *testing.T) {
type args struct {
request domainMessage.MarkAsReadRequest
}
tests := []struct {
name string
args args
errContains []string
}{
{
name: "should success with valid message id and phone",
args: args{request: domainMessage.MarkAsReadRequest{
MessageID: "3EB0789ABC123456",
Phone: "[email protected]",
}},
errContains: nil,
},
{
name: "should error with empty message id",
args: args{request: domainMessage.MarkAsReadRequest{
MessageID: "",
Phone: "[email protected]",
}},
errContains: []string{"message_id: cannot be blank"},
},
{
name: "should error with empty phone",
args: args{request: domainMessage.MarkAsReadRequest{
MessageID: "3EB0789ABC123456",
Phone: "",
}},
errContains: []string{"phone: cannot be blank"},
},
{
name: "should error with empty message id and phone",
args: args{request: domainMessage.MarkAsReadRequest{
MessageID: "",
Phone: "",
}},
errContains: []string{"message_id: cannot be blank", "phone: cannot be blank"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateMarkAsRead(context.Background(), tt.args.request)
if len(tt.errContains) == 0 {
assert.NoError(t, err)
} else {
assert.Error(t, err)
for _, msg := range tt.errContains {
assert.ErrorContains(t, err, msg)
}
}
})
}
}
func TestValidateReactMessage(t *testing.T) {
type args struct {
request domainMessage.ReactionRequest
}
tests := []struct {
name string
args args
err any
}{
{
name: "should success with valid phone, message id and emoji",
args: args{request: domainMessage.ReactionRequest{
Phone: "[email protected]",
MessageID: "3EB0789ABC123456",
Emoji: "π",
}},
err: nil,
},
{
name: "should success with heart emoji",
args: args{request: domainMessage.ReactionRequest{
Phone: "[email protected]",
MessageID: "3EB0789ABC123456",
Emoji: "β€οΈ",
}},
err: nil,
},
{
name: "should success with simple emoji",
args: args{request: domainMessage.ReactionRequest{
Phone: "[email protected]",
MessageID: "3EB0789ABC123456",
Emoji: "π",
}},
err: nil,
},
{
name: "should error with empty phone",
args: args{request: domainMessage.ReactionRequest{
Phone: "",
MessageID: "3EB0789ABC123456",
Emoji: "π",
}},
err: pkgError.ValidationError("phone: cannot be blank."),
},
{
name: "should error with empty message id",
args: args{request: domainMessage.ReactionRequest{
Phone: "[email protected]",
MessageID: "",
Emoji: "π",
}},
err: pkgError.ValidationError("message_id: cannot be blank."),
},
{
name: "should success with empty emoji (unreact)",
args: args{request: domainMessage.ReactionRequest{
Phone: "[email protected]",
MessageID: "3EB0789ABC123456",
Emoji: "",
}},
err: nil,
},
{
name: "should error with all empty fields",
args: args{request: domainMessage.ReactionRequest{
Phone: "",
MessageID: "",
Emoji: "",
}},
err: pkgError.ValidationError("message_id: cannot be blank; phone: cannot be blank."),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateReactMessage(context.Background(), tt.args.request)
if tt.err == nil {
assert.NoError(t, err)
} else if tt.name == "should error with all empty fields" {
assert.Error(t, err)
assert.Contains(t, err.Error(), "phone: cannot be blank")
assert.Contains(t, err.Error(), "message_id: cannot be blank")
} else {
assert.Equal(t, tt.err, err)
}
})
}
}
func TestValidateDeleteMessage(t *testing.T) {
type args struct {
request domainMessage.DeleteRequest
}
tests := []struct {
name string
args args
err any
}{
{
name: "should success with valid phone and message id",
args: args{request: domainMessage.DeleteRequest{
Phone: "[email protected]",
MessageID: "3EB0789ABC123456",
}},
err: nil,
},
{
name: "should error with empty phone",
args: args{request: domainMessage.DeleteRequest{
Phone: "",
MessageID: "3EB0789ABC123456",
}},
err: pkgError.ValidationError("phone: cannot be blank."),
},
{
name: "should error with empty message id",
args: args{request: domainMessage.DeleteRequest{
Phone: "[email protected]",
MessageID: "",
}},
err: pkgError.ValidationError("message_id: cannot be blank."),
},
{
name: "should error with empty phone and message id",
args: args{request: domainMessage.DeleteRequest{
Phone: "",
MessageID: "",
}},
err: pkgError.ValidationError("message_id: cannot be blank; phone: cannot be blank."),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateDeleteMessage(context.Background(), tt.args.request)
if tt.err == nil {
assert.NoError(t, err)
} else if tt.name == "should error with empty phone and message id" {
assert.Error(t, err)
assert.Contains(t, err.Error(), "message_id: cannot be blank")
assert.Contains(t, err.Error(), "phone: cannot be blank")
} else {
assert.Equal(t, tt.err, err)
}
})
}
}
func TestValidateStarMessage(t *testing.T) {
type args struct {
request domainMessage.StarRequest
}
tests := []struct {
name string
args args
err any
}{
{
name: "should success with valid phone, message id and starred true",
args: args{request: domainMessage.StarRequest{
Phone: "[email protected]",
MessageID: "3EB0789ABC123456",
IsStarred: true,
}},
err: nil,
},
{
name: "should success with valid phone, message id and starred false",
args: args{request: domainMessage.StarRequest{
Phone: "[email protected]",
MessageID: "3EB0789ABC123456",
IsStarred: false,
}},
err: nil,
},
{
name: "should error with empty phone",
args: args{request: domainMessage.StarRequest{
Phone: "",
MessageID: "3EB0789ABC123456",
IsStarred: true,
}},
err: pkgError.ValidationError("phone: cannot be blank."),
},
{
name: "should error with empty message id",
args: args{request: domainMessage.StarRequest{
Phone: "[email protected]",
MessageID: "",
IsStarred: true,
}},
err: pkgError.ValidationError("message_id: cannot be blank."),
},
{
name: "should error with empty phone and message id",
args: args{request: domainMessage.StarRequest{
Phone: "",
MessageID: "",
IsStarred: false,
}},
err: pkgError.ValidationError("message_id: cannot be blank; phone: cannot be blank."),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateStarMessage(context.Background(), tt.args.request)
if tt.err == nil {
assert.NoError(t, err)
} else {
assert.Equal(t, tt.err, err)
}
})
}
}