11//
2- // Copyright 2023 The Chainloop Authors.
2+ // Copyright 2024 The Chainloop Authors.
33//
44// Licensed under the Apache License, Version 2.0 (the "License");
55// you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@ package biz_test
1717
1818import (
1919 "context"
20+ "fmt"
2021 "testing"
2122 "time"
2223
@@ -54,6 +55,72 @@ func (s *workflowIntegrationTestSuite) TestContractLatestAvailable() {
5455 })
5556}
5657
58+ func (s * workflowIntegrationTestSuite ) TestCreate () {
59+ ctx := context .Background ()
60+ testCases := []struct {
61+ name string
62+ opts * biz.WorkflowCreateOpts
63+ wantErrMsg string
64+ }{
65+ {
66+ name : "org missing" ,
67+ opts : & biz.WorkflowCreateOpts {Name : "name" },
68+ wantErrMsg : "required" ,
69+ },
70+ {
71+ name : "name missing" ,
72+ opts : & biz.WorkflowCreateOpts {OrgID : s .org .ID },
73+ wantErrMsg : "required" ,
74+ },
75+ {
76+ name : "invalid name" ,
77+ opts : & biz.WorkflowCreateOpts {OrgID : s .org .ID , Name : "this/not/valid" },
78+ wantErrMsg : "RFC 1123" ,
79+ },
80+ {
81+ name : "another invalid name" ,
82+ opts : & biz.WorkflowCreateOpts {OrgID : s .org .ID , Name : "this-not Valid" },
83+ wantErrMsg : "RFC 1123" ,
84+ },
85+ {
86+ name : " invalid project name" ,
87+ opts : & biz.WorkflowCreateOpts {OrgID : s .org .ID , Name : "valid" , Project : "this-not Valid" },
88+ wantErrMsg : "RFC 1123" ,
89+ },
90+ {
91+ name : "non-existing contract" ,
92+ opts : & biz.WorkflowCreateOpts {OrgID : s .org .ID , Name : "name" , ContractID : uuid .Generate ().String ()},
93+ wantErrMsg : "not found" ,
94+ },
95+ {
96+ name : "can create it with just the name and the org" ,
97+ opts : & biz.WorkflowCreateOpts {OrgID : s .org .ID , Name : "name" },
98+ },
99+ {
100+ name : "with all items" ,
101+ opts : & biz.WorkflowCreateOpts {OrgID : s .org .ID , Name : "another-name" , Project : "project" , Team : "team" , Description : "description" },
102+ },
103+ }
104+
105+ for _ , tc := range testCases {
106+ s .Run (tc .name , func () {
107+ got , err := s .Workflow .Create (ctx , tc .opts )
108+ if tc .wantErrMsg != "" {
109+ s .ErrorContains (err , tc .wantErrMsg )
110+ return
111+ }
112+
113+ require .NoError (s .T (), err )
114+ s .NotEmpty (got .ID )
115+ s .NotEmpty (got .CreatedAt )
116+ s .Equal (tc .opts .Name , got .Name )
117+ s .Equal (tc .opts .Description , got .Description )
118+ s .Equal (tc .opts .Team , got .Team )
119+ s .Equal (tc .opts .Project , got .Project )
120+ })
121+ }
122+ }
123+
57124func (s * workflowIntegrationTestSuite ) TestUpdate () {
58125 ctx := context .Background ()
59126 const (
@@ -72,8 +139,15 @@ func (s *workflowIntegrationTestSuite) TestUpdate() {
72139 s .False (workflow .Public )
73140 })
74141
75- s .Run ("can't update a workflow in another org " , func () {
142+ s .Run ("can't update if no changes are provided " , func () {
76143 got , err := s .Workflow .Update (ctx , org2 .ID , workflow .ID .String (), nil )
144+ s .True (biz .IsErrValidation (err ))
145+ s .Error (err )
146+ s .Nil (got )
147+ })
148+
149+ s .Run ("can't update a workflow in another org" , func () {
150+ got , err := s .Workflow .Update (ctx , org2 .ID , workflow .ID .String (), & biz.WorkflowUpdateOpts {Name : toPtrS ("new-name" )})
77151 s .True (biz .IsNotFound (err ))
78152 s .Error (err )
79153 s .Nil (got )
@@ -82,62 +156,77 @@ func (s *workflowIntegrationTestSuite) TestUpdate() {
82156 testCases := []struct {
83157 name string
84158 // if not set, it will use the workflow we create on each run
85- id string
86- updates * biz.WorkflowUpdateOpts
87- want * biz.Workflow
88- wantErr bool
159+ id string
160+ updates * biz.WorkflowUpdateOpts
161+ want * biz.Workflow
162+ wantErr bool
163+ wantErrMsg string
89164 }{
90165 {
91166 name : "non existing workflow" ,
92167 id : uuid .Generate ().String (),
93- updates : & biz.WorkflowUpdateOpts {Name : toPtrS ("new name" )},
168+ updates : & biz.WorkflowUpdateOpts {Name : toPtrS ("new- name" )},
94169 wantErr : true ,
95170 },
96171 {
97172 name : "invalid uuid" ,
98173 id : "deadbeef" ,
99- updates : & biz.WorkflowUpdateOpts {Name : toPtrS ("new name" )},
174+ updates : & biz.WorkflowUpdateOpts {Name : toPtrS ("new- name" )},
100175 wantErr : true ,
101176 },
102177 {
103- name : "no updates" ,
104- want : & biz.Workflow {Name : name , Team : team , Project : project , Public : false , Description : description },
178+ name : "no updates" ,
179+ wantErr : true ,
180+ wantErrMsg : "no updates provided" ,
181+ },
182+ {
183+ name : "invalid name" ,
184+ wantErr : true ,
185+ wantErrMsg : "RFC 1123" ,
186+ updates : & biz.WorkflowUpdateOpts {Name : toPtrS (" no no " )},
187+ },
188+ {
189+ name : "invalid Project" ,
190+ wantErr : true ,
191+ wantErrMsg : "RFC 1123" ,
192+ updates : & biz.WorkflowUpdateOpts {Project : toPtrS (" no no " )},
105193 },
106194 {
107195 name : "update name" ,
108- updates : & biz.WorkflowUpdateOpts {Name : toPtrS ("new name" )},
109- want : & biz.Workflow {Name : "new name" , Description : description , Team : team , Project : project , Public : false },
196+ updates : & biz.WorkflowUpdateOpts {Name : toPtrS ("new- name" )},
197+ want : & biz.Workflow {Name : "new- name" , Description : description , Team : team , Project : project , Public : false },
110198 },
111199 {
112200 name : "update description" ,
113201 updates : & biz.WorkflowUpdateOpts {Description : toPtrS ("new description" )},
114- want : & biz.Workflow {Name : name , Description : "new description" , Team : team , Project : project , Public : false },
202+ want : & biz.Workflow {Description : "new description" , Team : team , Project : project , Public : false },
115203 },
116204 {
117205 name : "update visibility" ,
118206 updates : & biz.WorkflowUpdateOpts {Public : toPtrBool (true )},
119- want : & biz.Workflow {Name : name , Description : description , Team : team , Project : project , Public : true },
207+ want : & biz.Workflow {Description : description , Team : team , Project : project , Public : true },
120208 },
121209 {
122210 name : "update all options" ,
123- updates : & biz.WorkflowUpdateOpts {Name : toPtrS ("new name" ), Project : toPtrS ("new project" ), Team : toPtrS ("new team" ), Public : toPtrBool (true )},
124- want : & biz.Workflow {Name : "new name" , Description : description , Team : "new team" , Project : "new project" , Public : true },
211+ updates : & biz.WorkflowUpdateOpts {Name : toPtrS ("new- name-2 " ), Project : toPtrS ("new- project" ), Team : toPtrS ("new team" ), Public : toPtrBool (true )},
212+ want : & biz.Workflow {Name : "new- name-2 " , Description : description , Team : "new team" , Project : "new- project" , Public : true },
125213 },
126214 {
127215 name : "name can't be emptied" ,
128216 updates : & biz.WorkflowUpdateOpts {Name : toPtrS ("" )},
129- want : & biz. Workflow { Name : name , Team : team , Project : project , Description : description } ,
217+ wantErr : true ,
130218 },
131219 {
132220 name : "but other opts can" ,
133221 updates : & biz.WorkflowUpdateOpts {Team : toPtrS ("" ), Project : toPtrS ("" ), Description : toPtrS ("" )},
134- want : & biz.Workflow {Name : name , Team : "" , Project : "" , Description : "" },
222+ want : & biz.Workflow {Team : "" , Project : "" , Description : "" },
135223 },
136224 }
137225
138- for _ , tc := range testCases {
226+ for i , tc := range testCases {
139227 s .Run (tc .name , func () {
140- workflow , err := s .Workflow .Create (ctx , & biz.WorkflowCreateOpts {Description : description , Name : name , Team : team , Project : project , OrgID : s .org .ID })
228+ wfName := fmt .Sprintf ("%s-%d" , name , i )
229+ workflow , err := s .Workflow .Create (ctx , & biz.WorkflowCreateOpts {Description : description , Name : wfName , Team : team , Project : project , OrgID : s .org .ID })
141230 require .NoError (s .T (), err )
142231
143232 workflowID := tc .id
@@ -148,15 +237,25 @@ func (s *workflowIntegrationTestSuite) TestUpdate() {
148237 got , err := s .Workflow .Update (ctx , s .org .ID , workflowID , tc .updates )
149238 if tc .wantErr {
150239 s .Error (err )
240+ if tc .wantErrMsg != "" {
241+ s .Contains (err .Error (), tc .wantErrMsg )
242+ }
243+
151244 return
152245 }
153246 s .NoError (err )
154247
155248 if diff := cmp .Diff (tc .want , got ,
156- cmpopts .IgnoreFields (biz.Workflow {}, "CreatedAt" , "ID" , "OrgID" , "ContractID" , "ContractRevisionLatest" ),
249+ cmpopts .IgnoreFields (biz.Workflow {}, "Name" , " CreatedAt" , "ID" , "OrgID" , "ContractID" , "ContractRevisionLatest" ),
157250 ); diff != "" {
158251 s .Failf ("mismatch (-want +got):\n %s" , diff )
159252 }
253+
254+ if tc .want .Name != "" {
255+ s .Equal (tc .want .Name , got .Name )
256+ } else {
257+ s .Equal (wfName , got .Name )
258+ }
160259 })
161260 }
162261}
0 commit comments