1
1
package notifications_test
2
2
3
3
import (
4
+ "bytes"
4
5
"context"
6
+ _ "embed"
5
7
"encoding/json"
6
8
"fmt"
9
+ "go/ast"
10
+ "go/parser"
11
+ "go/token"
7
12
"net/http"
8
13
"net/http/httptest"
9
14
"net/url"
@@ -606,7 +611,42 @@ func TestNotifierPaused(t *testing.T) {
606
611
}, testutil .WaitShort , testutil .IntervalFast )
607
612
}
608
613
609
- func TestNotificationTemplatesBody (t * testing.T ) {
614
+ //go:embed events.go
615
+ var events []byte
616
+
617
+ // enumerateAllTemplates gets all the template names from the coderd/notifications/events.go file.
618
+ // TODO(dannyk): use code-generation to create a list of all templates: https://github.com/coder/team-coconut/issues/36
619
+ func enumerateAllTemplates (t * testing.T ) ([]string , error ) {
620
+ t .Helper ()
621
+
622
+ fset := token .NewFileSet ()
623
+
624
+ node , err := parser .ParseFile (fset , "" , bytes .NewBuffer (events ), parser .AllErrors )
625
+ if err != nil {
626
+ return nil , err
627
+ }
628
+
629
+ var out []string
630
+ // Traverse the AST and extract variable names.
631
+ ast .Inspect (node , func (n ast.Node ) bool {
632
+ // Check if the node is a declaration statement.
633
+ if decl , ok := n .(* ast.GenDecl ); ok && decl .Tok == token .VAR {
634
+ for _ , spec := range decl .Specs {
635
+ // Type assert the spec to a ValueSpec.
636
+ if valueSpec , ok := spec .(* ast.ValueSpec ); ok {
637
+ for _ , name := range valueSpec .Names {
638
+ out = append (out , name .String ())
639
+ }
640
+ }
641
+ }
642
+ }
643
+ return true
644
+ })
645
+
646
+ return out , nil
647
+ }
648
+
649
+ func TestNotificationTemplatesCanRender (t * testing.T ) {
610
650
t .Parallel ()
611
651
612
652
if ! dbtestutil .WillUsePostgres () {
@@ -647,10 +687,11 @@ func TestNotificationTemplatesBody(t *testing.T) {
647
687
payload : types.MessagePayload {
648
688
UserName : "bobby" ,
649
689
Labels : map [string ]string {
650
- "name" : "bobby-workspace" ,
651
- "reason" : "breached the template's threshold for inactivity" ,
652
- "initiator" : "autobuild" ,
653
- "dormancyHours" : "24" ,
690
+ "name" : "bobby-workspace" ,
691
+ "reason" : "breached the template's threshold for inactivity" ,
692
+ "initiator" : "autobuild" ,
693
+ "dormancyHours" : "24" ,
694
+ "timeTilDormant" : "24h" ,
654
695
},
655
696
},
656
697
},
@@ -660,8 +701,9 @@ func TestNotificationTemplatesBody(t *testing.T) {
660
701
payload : types.MessagePayload {
661
702
UserName : "bobby" ,
662
703
Labels : map [string ]string {
663
- "name" : "bobby-workspace" ,
664
- "template_version_name" : "1.0" ,
704
+ "name" : "bobby-workspace" ,
705
+ "template_version_name" : "1.0" ,
706
+ "template_version_message" : "template now includes catnip" ,
665
707
},
666
708
},
667
709
},
@@ -671,12 +713,46 @@ func TestNotificationTemplatesBody(t *testing.T) {
671
713
payload : types.MessagePayload {
672
714
UserName : "bobby" ,
673
715
Labels : map [string ]string {
674
- "name" : "bobby-workspace" ,
675
- "reason" : "template updated to new dormancy policy" ,
676
- "dormancyHours" : "24" ,
716
+ "name" : "bobby-workspace" ,
717
+ "reason" : "template updated to new dormancy policy" ,
718
+ "dormancyHours" : "24" ,
719
+ "timeTilDormant" : "24h" ,
720
+ },
721
+ },
722
+ },
723
+ {
724
+ name : "TemplateUserAccountCreated" ,
725
+ id : notifications .TemplateUserAccountCreated ,
726
+ payload : types.MessagePayload {
727
+ UserName : "bobby" ,
728
+ Labels : map [string ]string {
729
+ "created_account_name" : "bobby" ,
677
730
},
678
731
},
679
732
},
733
+ {
734
+ name : "TemplateUserAccountDeleted" ,
735
+ id : notifications .TemplateUserAccountDeleted ,
736
+ payload : types.MessagePayload {
737
+ UserName : "bobby" ,
738
+ Labels : map [string ]string {
739
+ "deleted_account_name" : "bobby" ,
740
+ },
741
+ },
742
+ },
743
+ }
744
+
745
+ allTemplates , err := enumerateAllTemplates (t )
746
+ require .NoError (t , err )
747
+ for _ , name := range allTemplates {
748
+ var found bool
749
+ for _ , tc := range tests {
750
+ if tc .name == name {
751
+ found = true
752
+ }
753
+ }
754
+
755
+ require .Truef (t , found , "could not find test case for %q" , name )
680
756
}
681
757
682
758
for _ , tc := range tests {
@@ -697,6 +773,7 @@ func TestNotificationTemplatesBody(t *testing.T) {
697
773
require .NoError (t , err , "failed to query body template for template:" , tc .id )
698
774
699
775
title , err := render .GoTemplate (titleTmpl , tc .payload , nil )
776
+ require .NotContainsf (t , title , render .NoValue , "template %q is missing a label value" , tc .name )
700
777
require .NoError (t , err , "failed to render notification title template" )
701
778
require .NotEmpty (t , title , "title should not be empty" )
702
779
0 commit comments