1
0
mirror of synced 2024-11-25 06:26:03 +03:00

test fixes

This commit is contained in:
Pavel 2023-11-10 13:54:06 +03:00
parent 55d16c4dff
commit 6a02d91fed
3 changed files with 202 additions and 31 deletions

1
mg-transport-api-client-go Symbolic link
View File

@ -0,0 +1 @@
/home/pavel/work/mg-transports/mg-transport-api-client-go

View File

@ -197,6 +197,34 @@ type TemplateHeader struct {
Content HeaderContent `json:"content"`
}
func (h *TemplateHeader) TextContent() *HeaderContentText {
if h.Content.HeaderContentType() != HeaderContentTypeText {
return nil
}
return h.Content.(*HeaderContentText)
}
func (h *TemplateHeader) DocumentContent() *HeaderContentDocument {
if h.Content.HeaderContentType() != HeaderContentTypeDocument {
return nil
}
return h.Content.(*HeaderContentDocument)
}
func (h *TemplateHeader) ImageContent() *HeaderContentImage {
if h.Content.HeaderContentType() != HeaderContentTypeImage {
return nil
}
return h.Content.(*HeaderContentImage)
}
func (h *TemplateHeader) VideoContent() *HeaderContentVideo {
if h.Content.HeaderContentType() != HeaderContentTypeVideo {
return nil
}
return h.Content.(*HeaderContentVideo)
}
func (h *TemplateHeader) UnmarshalJSON(value []byte) error {
var ScanType struct {
Content json.RawMessage `json:"content"`

View File

@ -64,51 +64,193 @@ func TestTemplateItem_UnmarshalJSON(t *testing.T) {
assert.Empty(t, emptyVariableResult.Text)
}
func TestUnmarshalMediaInteractiveTemplate(t *testing.T) {
func TestUnmarshalInteractiveTemplate_TextHeader(t *testing.T) {
var template Template
input := `{
"code":"aaa#bbb#ru",
"phone": "79252223456",
"channel_id": 1,
"headerParams": {
"textVars": [
"Johny",
"1234C"
],
"imageUrl": "http://example.com/intaro/d2354125",
"videoUrl": "http://example.com/intaro/d2222",
"documentUrl": "http://example.com/intaro/d4444"
"header": {
"content": {
"type": "text",
"body": "Hello, {{1}}!"
}
},
"footer": "Scooter",
"buttonParams": [
{
"type": "URL",
"urlParameter": "222ddd"
},
{
"type": "QUICK_REPLY",
"text": "Yes"
}
],
"buttons": {
"items": [
{
"type": "url",
"label": "Go to website",
"url": "222ddd"
},
{
"type": "plain",
"label": "Yes"
}
]
},
"verification_status": "approved"
}`
assert.NoError(t, json.Unmarshal([]byte(input), &template))
assert.Equal(t, "aaa#bbb#ru", template.Code)
assert.Equal(t, []string{"Johny", "1234C"}, template.Header.Content.TextVars)
assert.Equal(t, "http://example.com/intaro/d2354125", template.HeaderParams.ImageURL)
assert.Equal(t, "http://example.com/intaro/d2222", template.HeaderParams.VideoURL)
assert.Equal(t, "http://example.com/intaro/d4444", template.HeaderParams.DocumentURL)
assert.Equal(t, "Scooter", *template.Footer)
assert.Equal(t, "approved", template.VerificationStatus)
assert.Equal(t, "URL", template.ButtonParams[0].ButtonType)
assert.Equal(t, "222ddd", template.ButtonParams[0].URLParameter)
assert.Equal(t, "QUICK_REPLY", template.ButtonParams[1].ButtonType)
assert.Equal(t, "Yes", template.ButtonParams[1].Text)
assert.Equal(t, HeaderContentTypeText, template.Header.Content.HeaderContentType())
h := template.Header.TextContent()
assert.Equal(t, "Hello, {{1}}!", h.Body)
assert.Equal(t, "Scooter", template.Footer)
assert.Equal(t, TemplateStatusApproved, template.VerificationStatus)
assert.Equal(t, ButtonTypeUrl, template.Buttons.Items[0].ButtonType())
assert.Equal(t, "222ddd", template.Buttons.Items[0].(*UrlButton).Url)
assert.Equal(t, "Go to website", template.Buttons.Items[0].(*UrlButton).Label)
assert.Equal(t, ButtonTypePlain, template.Buttons.Items[1].ButtonType())
assert.Equal(t, "Yes", template.Buttons.Items[1].(*PlainButton).Label)
input = `{"footer": "Scooter"}`
template = Template{}
assert.NoError(t, json.Unmarshal([]byte(input), &template))
assert.Nil(t, template.HeaderParams)
assert.Empty(t, template.ButtonParams)
assert.Nil(t, template.Header)
assert.Empty(t, template.Buttons)
}
func TestUnmarshalInteractiveTemplate_DocumentHeader(t *testing.T) {
var template Template
input := `{
"code":"aaa#bbb#ru",
"phone": "79252223456",
"channel_id": 1,
"header": {
"content": {
"type": "document"
}
},
"footer": "Scooter",
"buttons": {
"items": [
{
"type": "url",
"label": "Go to website",
"url": "222ddd"
},
{
"type": "plain",
"label": "Yes"
}
]
},
"verification_status": "approved"
}`
assert.NoError(t, json.Unmarshal([]byte(input), &template))
assert.Equal(t, "aaa#bbb#ru", template.Code)
assert.Equal(t, HeaderContentTypeDocument, template.Header.Content.HeaderContentType())
assert.NotNil(t, template.Header.DocumentContent())
assert.Equal(t, "Scooter", template.Footer)
assert.Equal(t, TemplateStatusApproved, template.VerificationStatus)
assert.Equal(t, ButtonTypeUrl, template.Buttons.Items[0].ButtonType())
assert.Equal(t, "222ddd", template.Buttons.Items[0].(*UrlButton).Url)
assert.Equal(t, "Go to website", template.Buttons.Items[0].(*UrlButton).Label)
assert.Equal(t, ButtonTypePlain, template.Buttons.Items[1].ButtonType())
assert.Equal(t, "Yes", template.Buttons.Items[1].(*PlainButton).Label)
input = `{"footer": "Scooter"}`
template = Template{}
assert.NoError(t, json.Unmarshal([]byte(input), &template))
assert.Nil(t, template.Header)
assert.Empty(t, template.Buttons)
}
func TestUnmarshalInteractiveTemplate_ImageHeader(t *testing.T) {
var template Template
input := `{
"code":"aaa#bbb#ru",
"phone": "79252223456",
"channel_id": 1,
"header": {
"content": {
"type": "image"
}
},
"footer": "Scooter",
"buttons": {
"items": [
{
"type": "url",
"label": "Go to website",
"url": "222ddd"
},
{
"type": "plain",
"label": "Yes"
}
]
},
"verification_status": "approved"
}`
assert.NoError(t, json.Unmarshal([]byte(input), &template))
assert.Equal(t, "aaa#bbb#ru", template.Code)
assert.Equal(t, HeaderContentTypeImage, template.Header.Content.HeaderContentType())
assert.NotNil(t, template.Header.ImageContent())
assert.Equal(t, "Scooter", template.Footer)
assert.Equal(t, TemplateStatusApproved, template.VerificationStatus)
assert.Equal(t, ButtonTypeUrl, template.Buttons.Items[0].ButtonType())
assert.Equal(t, "222ddd", template.Buttons.Items[0].(*UrlButton).Url)
assert.Equal(t, "Go to website", template.Buttons.Items[0].(*UrlButton).Label)
assert.Equal(t, ButtonTypePlain, template.Buttons.Items[1].ButtonType())
assert.Equal(t, "Yes", template.Buttons.Items[1].(*PlainButton).Label)
input = `{"footer": "Scooter"}`
template = Template{}
assert.NoError(t, json.Unmarshal([]byte(input), &template))
assert.Nil(t, template.Header)
assert.Empty(t, template.Buttons)
}
func TestUnmarshalInteractiveTemplate_VideoHeader(t *testing.T) {
var template Template
input := `{
"code":"aaa#bbb#ru",
"phone": "79252223456",
"channel_id": 1,
"header": {
"content": {
"type": "video"
}
},
"footer": "Scooter",
"buttons": {
"items": [
{
"type": "url",
"label": "Go to website",
"url": "222ddd"
},
{
"type": "plain",
"label": "Yes"
}
]
},
"verification_status": "approved"
}`
assert.NoError(t, json.Unmarshal([]byte(input), &template))
assert.Equal(t, "aaa#bbb#ru", template.Code)
assert.Equal(t, HeaderContentTypeVideo, template.Header.Content.HeaderContentType())
assert.NotNil(t, template.Header.VideoContent())
assert.Equal(t, "Scooter", template.Footer)
assert.Equal(t, TemplateStatusApproved, template.VerificationStatus)
assert.Equal(t, ButtonTypeUrl, template.Buttons.Items[0].ButtonType())
assert.Equal(t, "222ddd", template.Buttons.Items[0].(*UrlButton).Url)
assert.Equal(t, "Go to website", template.Buttons.Items[0].(*UrlButton).Label)
assert.Equal(t, ButtonTypePlain, template.Buttons.Items[1].ButtonType())
assert.Equal(t, "Yes", template.Buttons.Items[1].(*PlainButton).Label)
input = `{"footer": "Scooter"}`
template = Template{}
assert.NoError(t, json.Unmarshal([]byte(input), &template))
assert.Nil(t, template.Header)
assert.Empty(t, template.Buttons)
}