From 6a02d91fed8c3c3acab5d5aca3c50e7d682130a6 Mon Sep 17 00:00:00 2001 From: Neur0toxine Date: Fri, 10 Nov 2023 13:54:06 +0300 Subject: [PATCH] test fixes --- mg-transport-api-client-go | 1 + v1/template.go | 28 +++++ v1/template_test.go | 204 +++++++++++++++++++++++++++++++------ 3 files changed, 202 insertions(+), 31 deletions(-) create mode 120000 mg-transport-api-client-go diff --git a/mg-transport-api-client-go b/mg-transport-api-client-go new file mode 120000 index 0000000..0ff901d --- /dev/null +++ b/mg-transport-api-client-go @@ -0,0 +1 @@ +/home/pavel/work/mg-transports/mg-transport-api-client-go \ No newline at end of file diff --git a/v1/template.go b/v1/template.go index 62474b8..968df5c 100644 --- a/v1/template.go +++ b/v1/template.go @@ -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"` diff --git a/v1/template_test.go b/v1/template_test.go index 1b9ac9e..43c1e73 100644 --- a/v1/template_test.go +++ b/v1/template_test.go @@ -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) }