@@ -970,13 +970,24 @@ func TestPreviousTemplateVersion(t *testing.T) {
970970 t .Parallel ()
971971 client := coderdtest .New (t , & coderdtest.Options {IncludeProvisionerDaemon : true })
972972 user := coderdtest .CreateFirstUser (t , client )
973- version := coderdtest .CreateTemplateVersion (t , client , user .OrganizationID , nil )
974- coderdtest .AwaitTemplateVersionJob (t , client , version .ID )
973+
974+ // Create two templates to be sure it is not returning a previous version
975+ // from another template
976+ templateAVersion1 := coderdtest .CreateTemplateVersion (t , client , user .OrganizationID , nil )
977+ coderdtest .CreateTemplate (t , client , user .OrganizationID , templateAVersion1 .ID )
978+ coderdtest .AwaitTemplateVersionJob (t , client , templateAVersion1 .ID )
979+ // Create two versions for the template B to be sure if we try to get the
980+ // previous version of the first version it will returns a 404
981+ templateBVersion1 := coderdtest .CreateTemplateVersion (t , client , user .OrganizationID , nil )
982+ templateB := coderdtest .CreateTemplate (t , client , user .OrganizationID , templateBVersion1 .ID )
983+ coderdtest .AwaitTemplateVersionJob (t , client , templateBVersion1 .ID )
984+ templateBVersion2 := coderdtest .UpdateTemplateVersion (t , client , user .OrganizationID , nil , templateB .ID )
985+ coderdtest .AwaitTemplateVersionJob (t , client , templateBVersion2 .ID )
975986
976987 ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
977988 defer cancel ()
978989
979- _ , err := client .PreviousTemplateVersion (ctx , user .OrganizationID , version .Name )
990+ _ , err := client .PreviousTemplateVersion (ctx , user .OrganizationID , templateBVersion1 .Name )
980991 var apiErr * codersdk.Error
981992 require .ErrorAs (t , err , & apiErr )
982993 require .Equal (t , http .StatusNotFound , apiErr .StatusCode ())
@@ -986,17 +997,25 @@ func TestPreviousTemplateVersion(t *testing.T) {
986997 t .Parallel ()
987998 client := coderdtest .New (t , & coderdtest.Options {IncludeProvisionerDaemon : true })
988999 user := coderdtest .CreateFirstUser (t , client )
989- previousVersion := coderdtest .CreateTemplateVersion (t , client , user .OrganizationID , nil )
990- coderdtest .AwaitTemplateVersionJob (t , client , previousVersion .ID )
991- template := coderdtest .CreateTemplate (t , client , user .OrganizationID , previousVersion .ID )
992- latestVersion := coderdtest .UpdateTemplateVersion (t , client , user .OrganizationID , nil , template .ID )
993- coderdtest .AwaitTemplateVersionJob (t , client , latestVersion .ID )
1000+
1001+ // Create two templates to be sure it is not returning a previous version
1002+ // from another template
1003+ templateAVersion1 := coderdtest .CreateTemplateVersion (t , client , user .OrganizationID , nil )
1004+ coderdtest .CreateTemplate (t , client , user .OrganizationID , templateAVersion1 .ID )
1005+ coderdtest .AwaitTemplateVersionJob (t , client , templateAVersion1 .ID )
1006+ // Create two versions for the template B so we can try to get the previous
1007+ // version of version 2
1008+ templateBVersion1 := coderdtest .CreateTemplateVersion (t , client , user .OrganizationID , nil )
1009+ templateB := coderdtest .CreateTemplate (t , client , user .OrganizationID , templateBVersion1 .ID )
1010+ coderdtest .AwaitTemplateVersionJob (t , client , templateBVersion1 .ID )
1011+ templateBVersion2 := coderdtest .UpdateTemplateVersion (t , client , user .OrganizationID , nil , templateB .ID )
1012+ coderdtest .AwaitTemplateVersionJob (t , client , templateBVersion2 .ID )
9941013
9951014 ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
9961015 defer cancel ()
9971016
998- result , err := client .PreviousTemplateVersion (ctx , user .OrganizationID , latestVersion .Name )
1017+ result , err := client .PreviousTemplateVersion (ctx , user .OrganizationID , templateBVersion2 .Name )
9991018 require .NoError (t , err )
1000- require .Equal (t , previousVersion .ID , result .ID )
1019+ require .Equal (t , templateBVersion1 .ID , result .ID )
10011020 })
10021021}
0 commit comments