@@ -6,8 +6,10 @@ import (
6
6
"os"
7
7
"path/filepath"
8
8
"runtime"
9
+ "strconv"
9
10
"strings"
10
11
"testing"
12
+ "time"
11
13
12
14
"github.com/google/uuid"
13
15
"github.com/stretchr/testify/assert"
@@ -708,6 +710,152 @@ func TestTemplatePush(t *testing.T) {
708
710
require .NotEqual (t ,uuid .Nil ,template .ActiveVersionID )
709
711
})
710
712
})
713
+
714
+ t .Run ("EditMetadata" ,func (t * testing.T ) {
715
+ t .Parallel ()
716
+ client := coderdtest .New (t ,& coderdtest.Options {IncludeProvisionerDaemon :true })
717
+ owner := coderdtest .CreateFirstUser (t ,client )
718
+ templateAdmin ,_ := coderdtest .CreateAnotherUser (t ,client ,owner .OrganizationID ,rbac .RoleTemplateAdmin ())
719
+ version := coderdtest .CreateTemplateVersion (t ,client ,owner .OrganizationID ,nil )
720
+ _ = coderdtest .AwaitTemplateVersionJobCompleted (t ,client ,version .ID )
721
+
722
+ template := coderdtest .CreateTemplate (t ,client ,owner .OrganizationID ,version .ID )
723
+
724
+ // Test the cli command.
725
+ source := clitest .CreateTemplateVersionSource (t ,& echo.Responses {
726
+ Parse :echo .ParseComplete ,
727
+ ProvisionApply :echo .ApplyComplete ,
728
+ })
729
+
730
+ name := "new-template-name"
731
+ displayName := "New Display Name 789"
732
+ desc := "lorem ipsum dolor sit amet et cetera"
733
+ icon := "/icon/new-icon.png"
734
+ defaultTTL := 12 * time .Hour
735
+ allowUserCancelWorkspaceJobs := false
736
+
737
+ inv ,root := clitest .New (t ,
738
+ "templates" ,
739
+ "push" ,
740
+ template .Name ,
741
+ "--directory" ,source ,
742
+ "--test.provisioner" ,string (database .ProvisionerTypeEcho ),
743
+ "--name" ,name ,
744
+ "--display-name" ,displayName ,
745
+ "--description" ,desc ,
746
+ "--icon" ,icon ,
747
+ "--default-ttl" ,defaultTTL .String (),
748
+ "--allow-user-cancel-workspace-jobs=" + strconv .FormatBool (allowUserCancelWorkspaceJobs ),
749
+ )
750
+ clitest .SetupConfig (t ,templateAdmin ,root )
751
+ pty := ptytest .New (t ).Attach (inv )
752
+
753
+ execDone := make (chan error )
754
+ go func () {
755
+ execDone <- inv .Run ()
756
+ }()
757
+
758
+ matches := []struct {
759
+ match string
760
+ write string
761
+ }{
762
+ {match :"Upload" ,write :"yes" },
763
+ }
764
+ for _ ,m := range matches {
765
+ pty .ExpectMatch (m .match )
766
+ pty .WriteLine (m .write )
767
+ }
768
+
769
+ require .NoError (t ,<- execDone )
770
+
771
+ // Assert that the template version changed.
772
+ templateVersions ,err := client .TemplateVersionsByTemplate (context .Background (), codersdk.TemplateVersionsByTemplateRequest {
773
+ TemplateID :template .ID ,
774
+ })
775
+ require .NoError (t ,err )
776
+ assert .Len (t ,templateVersions ,2 )
777
+ assert .NotEqual (t ,template .ActiveVersionID ,templateVersions [1 ].ID )
778
+ require .Equal (t ,name ,templateVersions [1 ].Name )
779
+
780
+ // Assert that the template metadata changed.
781
+ updated ,err := client .Template (context .Background (),template .ID )
782
+ require .NoError (t ,err )
783
+ assert .Equal (t ,template .Name ,updated .Name )
784
+ assert .Equal (t ,displayName ,updated .DisplayName )
785
+ assert .Equal (t ,desc ,updated .Description )
786
+ assert .Equal (t ,icon ,updated .Icon )
787
+ assert .Equal (t ,defaultTTL .Milliseconds (),updated .DefaultTTLMillis )
788
+ assert .Equal (t ,allowUserCancelWorkspaceJobs ,updated .AllowUserCancelWorkspaceJobs )
789
+ })
790
+
791
+ t .Run ("EditMetadataNoSideEffects" ,func (t * testing.T ) {
792
+ t .Parallel ()
793
+ client := coderdtest .New (t ,& coderdtest.Options {IncludeProvisionerDaemon :true })
794
+ owner := coderdtest .CreateFirstUser (t ,client )
795
+ templateAdmin ,_ := coderdtest .CreateAnotherUser (t ,client ,owner .OrganizationID ,rbac .RoleTemplateAdmin ())
796
+ version := coderdtest .CreateTemplateVersion (t ,client ,owner .OrganizationID ,nil )
797
+ _ = coderdtest .AwaitTemplateVersionJobCompleted (t ,client ,version .ID )
798
+
799
+ template := coderdtest .CreateTemplate (t ,client ,owner .OrganizationID ,version .ID )
800
+
801
+ // Test the cli command.
802
+ source := clitest .CreateTemplateVersionSource (t ,& echo.Responses {
803
+ Parse :echo .ParseComplete ,
804
+ ProvisionApply :echo .ApplyComplete ,
805
+ })
806
+
807
+ desc := "lorem ipsum dolor sit amet et cetera"
808
+
809
+ inv ,root := clitest .New (t ,
810
+ "templates" ,
811
+ "push" ,
812
+ template .Name ,
813
+ "--directory" ,source ,
814
+ "--test.provisioner" ,string (database .ProvisionerTypeEcho ),
815
+ "--description" ,desc ,
816
+ )
817
+ clitest .SetupConfig (t ,templateAdmin ,root )
818
+ pty := ptytest .New (t ).Attach (inv )
819
+
820
+ execDone := make (chan error )
821
+ go func () {
822
+ execDone <- inv .Run ()
823
+ }()
824
+
825
+ matches := []struct {
826
+ match string
827
+ write string
828
+ }{
829
+ {match :"Upload" ,write :"yes" },
830
+ }
831
+ for _ ,m := range matches {
832
+ pty .ExpectMatch (m .match )
833
+ pty .WriteLine (m .write )
834
+ }
835
+
836
+ require .NoError (t ,<- execDone )
837
+
838
+ // Assert that the template version changed.
839
+ templateVersions ,err := client .TemplateVersionsByTemplate (context .Background (), codersdk.TemplateVersionsByTemplateRequest {
840
+ TemplateID :template .ID ,
841
+ })
842
+ require .NoError (t ,err )
843
+ assert .Len (t ,templateVersions ,2 )
844
+ assert .NotEqual (t ,template .ActiveVersionID ,templateVersions [1 ].ID )
845
+
846
+ // Assert that the template metadata changed.
847
+ updated ,err := client .Template (context .Background (),template .ID )
848
+ require .NoError (t ,err )
849
+ // Changed
850
+ assert .Equal (t ,desc ,updated .Description )
851
+
852
+ // Should not change
853
+ assert .Equal (t ,template .Name ,updated .Name )
854
+ assert .Equal (t ,template .DisplayName ,updated .DisplayName )
855
+ assert .Equal (t ,template .Icon ,updated .Icon )
856
+ assert .Equal (t ,template .DefaultTTLMillis ,updated .DefaultTTLMillis )
857
+ assert .Equal (t ,template .AllowUserCancelWorkspaceJobs ,updated .AllowUserCancelWorkspaceJobs )
858
+ })
711
859
}
712
860
713
861
func createEchoResponsesWithTemplateVariables (templateVariables []* proto.TemplateVariable )* echo.Responses {