github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/tests/models/template_test.go (about)

     1  // This file is part of the Smart Home
     2  // Program complex distribution https://github.com/e154/smart-home
     3  // Copyright (C) 2016-2023, Filippov Alex
     4  //
     5  // This library is free software: you can redistribute it and/or
     6  // modify it under the terms of the GNU Lesser General Public
     7  // License as published by the Free Software Foundation; either
     8  // version 3 of the License, or (at your option) any later version.
     9  //
    10  // This library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13  // Library General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public
    16  // License along with this library.  If not, see
    17  // <https://www.gnu.org/licenses/>.
    18  
    19  package models
    20  
    21  import (
    22  	"context"
    23  	"fmt"
    24  	"testing"
    25  
    26  	"github.com/e154/smart-home/adaptors"
    27  	"github.com/e154/smart-home/common"
    28  	"github.com/e154/smart-home/endpoint"
    29  	m "github.com/e154/smart-home/models"
    30  	"github.com/e154/smart-home/system/migrations"
    31  	. "github.com/smartystreets/goconvey/convey"
    32  )
    33  
    34  func TestTemplate(t *testing.T) {
    35  
    36  	const subject = "Lorem ipsum dolor sit amet"
    37  	const body = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
    38  
    39  	Convey("add user", t, func(ctx C) {
    40  		_ = container.Invoke(func(adaptors *adaptors.Adaptors,
    41  			migrations *migrations.Migrations,
    42  			endpoint *endpoint.Endpoint) {
    43  
    44  			// clear database
    45  			// ------------------------------------------------
    46  			err := migrations.Purge()
    47  			So(err, ShouldBeNil)
    48  
    49  			// add templates
    50  			// ------------------------------------------------
    51  			templates := []*m.Template{
    52  				{
    53  					Name:       "main",
    54  					Content:    "[message:block]",
    55  					Status:     m.TemplateStatusActive,
    56  					Type:       m.TemplateTypeItem,
    57  					ParentName: nil,
    58  				},
    59  				{
    60  					Name:       "message",
    61  					Content:    "[title:block][body:block]",
    62  					Status:     m.TemplateStatusActive,
    63  					Type:       m.TemplateTypeItem,
    64  					ParentName: common.String("main"),
    65  				},
    66  				{
    67  					Name:       "title",
    68  					Content:    "[title:content] [var1]",
    69  					Status:     m.TemplateStatusActive,
    70  					Type:       m.TemplateTypeItem,
    71  					ParentName: common.String("message"),
    72  				},
    73  				{
    74  					Name:       "body",
    75  					Content:    "[body:content] [var2]",
    76  					Status:     m.TemplateStatusActive,
    77  					Type:       m.TemplateTypeItem,
    78  					ParentName: common.String("message"),
    79  				},
    80  				{
    81  					Name: "template1",
    82  					Content: fmt.Sprintf(`{
    83    "items": [
    84      "title",
    85      "body"
    86    ],
    87    "title": "%s",
    88    "fields": [
    89      {
    90        "name": "title",
    91        "value": "Lorem ipsum dolor sit amet,"
    92      },
    93      {
    94        "name": "body",
    95        "value": ", sed do eiusmod tempor incididunt"
    96      }
    97    ]
    98  }`, subject),
    99  					Status:     m.TemplateStatusActive,
   100  					Type:       m.TemplateTypeTemplate,
   101  					ParentName: nil,
   102  				},
   103  				{
   104  					Name:       "sms_body",
   105  					Content:    "[code:block]",
   106  					Status:     m.TemplateStatusActive,
   107  					Type:       m.TemplateTypeItem,
   108  					ParentName: nil,
   109  				},
   110  				{
   111  					Name:       "code",
   112  					Content:    "[code:content] [code]",
   113  					Status:     m.TemplateStatusActive,
   114  					Type:       m.TemplateTypeItem,
   115  					ParentName: common.String("sms_body"),
   116  				},
   117  				{
   118  					Name: "template2",
   119  					Content: `{
   120   "items": [
   121     "code"
   122   ],
   123   "title": "",
   124   "fields": [
   125  	{
   126       "name": "code",
   127       "value": "Activate code:"
   128     }
   129  ]
   130  }`,
   131  					Status:     m.TemplateStatusActive,
   132  					Type:       m.TemplateTypeTemplate,
   133  					ParentName: nil,
   134  				},
   135  				{
   136  					Name:       "sms_warning",
   137  					Content:    "some warning message",
   138  					Status:     m.TemplateStatusActive,
   139  					Type:       m.TemplateTypeItem,
   140  					ParentName: nil,
   141  				},
   142  				{
   143  					Name: "template3",
   144  					Content: `{
   145   "items": [
   146     "sms_warning"
   147   ],
   148   "title": "",
   149   "fields": []
   150  }`,
   151  					Status:     m.TemplateStatusActive,
   152  					Type:       m.TemplateTypeTemplate,
   153  					ParentName: nil,
   154  				},
   155  			}
   156  
   157  			for _, template := range templates {
   158  				err := adaptors.Template.UpdateOrCreate(context.Background(), template)
   159  				So(err, ShouldBeNil)
   160  			}
   161  
   162  			// Lorem ipsum dolor sit amet
   163  			// ------------------------------------------------
   164  			render, err := adaptors.Template.Render(context.Background(), "template1", map[string]interface{}{
   165  				"var1": "consectetur adipiscing elit",
   166  				"var2": "ut labore et dolore magna aliqua.",
   167  			})
   168  			So(err, ShouldBeNil)
   169  			So(render.Subject, ShouldEqual, subject)
   170  			So(render.Body, ShouldEqual, body)
   171  
   172  			// Activate code: 12345
   173  			// ------------------------------------------------
   174  			render, err = adaptors.Template.Render(context.Background(), "template2", map[string]interface{}{
   175  				"code": 12345,
   176  			})
   177  			So(err, ShouldBeNil)
   178  			So(render.Subject, ShouldEqual, "")
   179  			So(render.Body, ShouldEqual, "Activate code: 12345")
   180  
   181  			// warning message
   182  			// ------------------------------------------------
   183  			render, err = adaptors.Template.Render(context.Background(), "template3", nil)
   184  			So(err, ShouldBeNil)
   185  			So(render.Subject, ShouldEqual, "")
   186  			So(render.Body, ShouldEqual, "some warning message")
   187  
   188  			//...
   189  		})
   190  	})
   191  }