go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/luci_notify/notify/emailgen_test.go (about)

     1  // Copyright 2018 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package notify
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	buildbucketpb "go.chromium.org/luci/buildbucket/proto"
    22  	"go.chromium.org/luci/gae/impl/memory"
    23  	"go.chromium.org/luci/gae/service/datastore"
    24  	"go.chromium.org/luci/server/caching"
    25  
    26  	notifypb "go.chromium.org/luci/luci_notify/api/config"
    27  	"go.chromium.org/luci/luci_notify/common"
    28  	"go.chromium.org/luci/luci_notify/config"
    29  
    30  	. "github.com/smartystreets/goconvey/convey"
    31  )
    32  
    33  func TestEmailGen(t *testing.T) {
    34  	t.Parallel()
    35  
    36  	Convey(`bundle`, t, func() {
    37  		c := memory.Use(context.Background())
    38  		c = common.SetAppIDForTest(c, "luci-config")
    39  		c = caching.WithEmptyProcessCache(c)
    40  
    41  		chromium := &config.Project{Name: "chromium", Revision: "deadbeef"}
    42  		chromiumKey := datastore.KeyForObj(c, chromium)
    43  		So(datastore.Put(c, chromium), ShouldBeNil)
    44  
    45  		templates := []*config.EmailTemplate{
    46  			{
    47  				ProjectKey:          chromiumKey,
    48  				Name:                "default",
    49  				SubjectTextTemplate: "Build {{.Build.Id}} completed",
    50  				BodyHTMLTemplate:    `Build {{.Build.Id}} completed with status {{.Build.Status}}`,
    51  			},
    52  			{
    53  				ProjectKey:          chromiumKey,
    54  				Name:                "using_other_files",
    55  				SubjectTextTemplate: "",
    56  				BodyHTMLTemplate: `
    57  Reusing templates from another files.
    58  {{template "inlineEntireFile" .}}
    59  {{template "steps" .}}`,
    60  			},
    61  			{
    62  				ProjectKey:          chromiumKey,
    63  				Name:                "inlineEntireFile",
    64  				SubjectTextTemplate: "this file is shared",
    65  				BodyHTMLTemplate:    `Build {{.Build.Id}}`,
    66  			},
    67  			{
    68  				ProjectKey:          chromiumKey,
    69  				Name:                "shared",
    70  				SubjectTextTemplate: "this file is shared",
    71  				BodyHTMLTemplate:    `{{define "steps"}}steps of build {{.Build.Id}} go here{{end}}`,
    72  			},
    73  			{
    74  				ProjectKey:          chromiumKey,
    75  				Name:                "bad",
    76  				SubjectTextTemplate: "bad template",
    77  				BodyHTMLTemplate:    `{{.FieldDoesNotExist}}`,
    78  			},
    79  		}
    80  		So(datastore.Put(c, templates), ShouldBeNil)
    81  		datastore.GetTestable(c).CatchupIndexes()
    82  
    83  		bundle, err := getBundle(c, chromium.Name)
    84  		So(err, ShouldBeNil)
    85  
    86  		Convey("bundles are cached", func() {
    87  			secondBundle, err := getBundle(c, chromium.Name)
    88  			So(err, ShouldBeNil)
    89  			So(secondBundle, ShouldEqual, bundle) // pointers match
    90  		})
    91  
    92  		Convey("caching honors revision", func() {
    93  			chromium.Revision = "badcoffee"
    94  			So(datastore.Put(c, chromium), ShouldBeNil)
    95  			secondBundle, err := getBundle(c, chromium.Name)
    96  			So(err, ShouldBeNil)
    97  			So(secondBundle, ShouldNotEqual, bundle) // pointers mismatch, new bundle
    98  		})
    99  
   100  		Convey(`GenerateEmail`, func() {
   101  			input := &notifypb.TemplateInput{
   102  				BuildbucketHostname: "buildbucket.example.com",
   103  				Build: &buildbucketpb.Build{
   104  					Id: 54,
   105  					Builder: &buildbucketpb.BuilderID{
   106  						Project: "chromium",
   107  						Bucket:  "ci",
   108  						Builder: "linux-rel",
   109  					},
   110  					Status: buildbucketpb.Status_SUCCESS,
   111  				},
   112  			}
   113  			Convey("simple template", func() {
   114  				subject, body := bundle.GenerateEmail("default", input)
   115  				So(subject, ShouldEqual, "Build 54 completed")
   116  				So(body, ShouldEqual, "Build 54 completed with status SUCCESS")
   117  			})
   118  
   119  			Convey("template using other files", func() {
   120  				_, body := bundle.GenerateEmail("using_other_files", input)
   121  				So(body, ShouldEqual, `
   122  Reusing templates from another files.
   123  Build 54
   124  steps of build 54 go here`)
   125  			})
   126  
   127  			Convey("error", func() {
   128  				_, body := bundle.GenerateEmail("bad", input)
   129  				So(body, ShouldContainSubstring, "spartan")
   130  			})
   131  		})
   132  	})
   133  }