@@ -29,27 +29,28 @@ func TestTaskCreate(t *testing.T) {
2929taskCreatedAt = time .Now ()
3030
3131organizationID = uuid .New ()
32+ anotherOrganizationID = uuid .New ()
3233templateID = uuid .New ()
3334templateVersionID = uuid .New ()
3435templateVersionPresetID = uuid .New ()
3536)
3637
37- templateAndVersionFoundHandler := func (t * testing.T ,ctx context.Context ,templateName ,templateVersionName ,presetName ,prompt string ) http.HandlerFunc {
38+ templateAndVersionFoundHandler := func (t * testing.T ,ctx context.Context ,orgID uuid. UUID , templateName ,templateVersionName ,presetName ,prompt string ) http.HandlerFunc {
3839t .Helper ()
3940
4041return func (w http.ResponseWriter ,r * http.Request ) {
4142switch r .URL .Path {
4243case "/api/v2/users/me/organizations" :
4344httpapi .Write (ctx ,w ,http .StatusOK , []codersdk.Organization {
4445{MinimalOrganization : codersdk.MinimalOrganization {
45- ID :organizationID ,
46+ ID :orgID ,
4647}},
4748})
48- case fmt .Sprintf ("/api/v2/organizations/%s/templates/my-template/versions/my-template-version" ,organizationID ):
49+ case fmt .Sprintf ("/api/v2/organizations/%s/templates/my-template/versions/my-template-version" ,orgID ):
4950httpapi .Write (ctx ,w ,http .StatusOK , codersdk.TemplateVersion {
5051ID :templateVersionID ,
5152})
52- case fmt .Sprintf ("/api/v2/organizations/%s/templates/my-template" ,organizationID ):
53+ case fmt .Sprintf ("/api/v2/organizations/%s/templates/my-template" ,orgID ):
5354httpapi .Write (ctx ,w ,http .StatusOK , codersdk.Template {
5455ID :templateID ,
5556ActiveVersionID :templateVersionID ,
@@ -94,62 +95,62 @@ func TestTaskCreate(t *testing.T) {
9495handler func (t * testing.T ,ctx context.Context ) http.HandlerFunc
9596}{
9697{
97- args : []string {"my-template@my-template-version" ,"--input" ,"my custom prompt" },
98+ args : []string {"my-template@my-template-version" ,"--input" ,"my custom prompt" , "--org" , organizationID . String () },
9899expectOutput :fmt .Sprintf ("The task %s has been created at %s!" ,cliui .Keyword ("task-wild-goldfish-27" ),cliui .Timestamp (taskCreatedAt )),
99100handler :func (t * testing.T ,ctx context.Context ) http.HandlerFunc {
100- return templateAndVersionFoundHandler (t ,ctx ,"my-template" ,"my-template-version" ,"" ,"my custom prompt" )
101+ return templateAndVersionFoundHandler (t ,ctx ,organizationID , "my-template" ,"my-template-version" ,"" ,"my custom prompt" )
101102},
102103},
103104{
104- args : []string {"my-template" ,"--input" ,"my custom prompt" },
105+ args : []string {"my-template" ,"--input" ,"my custom prompt" , "--org" , organizationID . String () },
105106env : []string {"CODER_TASK_TEMPLATE_VERSION=my-template-version" },
106107expectOutput :fmt .Sprintf ("The task %s has been created at %s!" ,cliui .Keyword ("task-wild-goldfish-27" ),cliui .Timestamp (taskCreatedAt )),
107108handler :func (t * testing.T ,ctx context.Context ) http.HandlerFunc {
108- return templateAndVersionFoundHandler (t ,ctx ,"my-template" ,"my-template-version" ,"" ,"my custom prompt" )
109+ return templateAndVersionFoundHandler (t ,ctx ,organizationID , "my-template" ,"my-template-version" ,"" ,"my custom prompt" )
109110},
110111},
111112{
112- args : []string {"--input" ,"my custom prompt" },
113+ args : []string {"--input" ,"my custom prompt" , "--org" , organizationID . String () },
113114env : []string {"CODER_TASK_TEMPLATE_NAME=my-template" ,"CODER_TASK_TEMPLATE_VERSION=my-template-version" },
114115expectOutput :fmt .Sprintf ("The task %s has been created at %s!" ,cliui .Keyword ("task-wild-goldfish-27" ),cliui .Timestamp (taskCreatedAt )),
115116handler :func (t * testing.T ,ctx context.Context ) http.HandlerFunc {
116- return templateAndVersionFoundHandler (t ,ctx ,"my-template" ,"my-template-version" ,"" ,"my custom prompt" )
117+ return templateAndVersionFoundHandler (t ,ctx ,organizationID , "my-template" ,"my-template-version" ,"" ,"my custom prompt" )
117118},
118119},
119120{
120- env : []string {"CODER_TASK_TEMPLATE_NAME=my-template" ,"CODER_TASK_TEMPLATE_VERSION=my-template-version" ,"CODER_TASK_INPUT=my custom prompt" },
121+ env : []string {"CODER_TASK_TEMPLATE_NAME=my-template" ,"CODER_TASK_TEMPLATE_VERSION=my-template-version" ,"CODER_TASK_INPUT=my custom prompt" , "CODER_ORGANIZATION=" + organizationID . String () },
121122expectOutput :fmt .Sprintf ("The task %s has been created at %s!" ,cliui .Keyword ("task-wild-goldfish-27" ),cliui .Timestamp (taskCreatedAt )),
122123handler :func (t * testing.T ,ctx context.Context ) http.HandlerFunc {
123- return templateAndVersionFoundHandler (t ,ctx ,"my-template" ,"my-template-version" ,"" ,"my custom prompt" )
124+ return templateAndVersionFoundHandler (t ,ctx ,organizationID , "my-template" ,"my-template-version" ,"" ,"my custom prompt" )
124125},
125126},
126127{
127- args : []string {"my-template" ,"--input" ,"my custom prompt" },
128+ args : []string {"my-template" ,"--input" ,"my custom prompt" , "--org" , organizationID . String () },
128129expectOutput :fmt .Sprintf ("The task %s has been created at %s!" ,cliui .Keyword ("task-wild-goldfish-27" ),cliui .Timestamp (taskCreatedAt )),
129130handler :func (t * testing.T ,ctx context.Context ) http.HandlerFunc {
130- return templateAndVersionFoundHandler (t ,ctx ,"my-template" ,"" ,"" ,"my custom prompt" )
131+ return templateAndVersionFoundHandler (t ,ctx ,organizationID , "my-template" ,"" ,"" ,"my custom prompt" )
131132},
132133},
133134{
134- args : []string {"my-template" ,"--input" ,"my custom prompt" ,"--preset" ,"my-preset" },
135+ args : []string {"my-template" ,"--input" ,"my custom prompt" ,"--preset" ,"my-preset" , "--org" , organizationID . String () },
135136expectOutput :fmt .Sprintf ("The task %s has been created at %s!" ,cliui .Keyword ("task-wild-goldfish-27" ),cliui .Timestamp (taskCreatedAt )),
136137handler :func (t * testing.T ,ctx context.Context ) http.HandlerFunc {
137- return templateAndVersionFoundHandler (t ,ctx ,"my-template" ,"" ,"my-preset" ,"my custom prompt" )
138+ return templateAndVersionFoundHandler (t ,ctx ,organizationID , "my-template" ,"" ,"my-preset" ,"my custom prompt" )
138139},
139140},
140141{
141142args : []string {"my-template" ,"--input" ,"my custom prompt" },
142143env : []string {"CODER_TASK_PRESET_NAME=my-preset" },
143144expectOutput :fmt .Sprintf ("The task %s has been created at %s!" ,cliui .Keyword ("task-wild-goldfish-27" ),cliui .Timestamp (taskCreatedAt )),
144145handler :func (t * testing.T ,ctx context.Context ) http.HandlerFunc {
145- return templateAndVersionFoundHandler (t ,ctx ,"my-template" ,"" ,"my-preset" ,"my custom prompt" )
146+ return templateAndVersionFoundHandler (t ,ctx ,organizationID , "my-template" ,"" ,"my-preset" ,"my custom prompt" )
146147},
147148},
148149{
149150args : []string {"my-template" ,"--input" ,"my custom prompt" ,"--preset" ,"not-real-preset" },
150151expectError :`preset "not-real-preset" not found` ,
151152handler :func (t * testing.T ,ctx context.Context ) http.HandlerFunc {
152- return templateAndVersionFoundHandler (t ,ctx ,"my-template" ,"" ,"my-preset" ,"my custom prompt" )
153+ return templateAndVersionFoundHandler (t ,ctx ,organizationID , "my-template" ,"" ,"my-preset" ,"my custom prompt" )
153154},
154155},
155156{
@@ -173,7 +174,7 @@ func TestTaskCreate(t *testing.T) {
173174},
174175},
175176{
176- args : []string {"not-real-template" ,"--input" ,"my custom prompt" },
177+ args : []string {"not-real-template" ,"--input" ,"my custom prompt" , "--org" , organizationID . String () },
177178expectError :httpapi .ResourceNotFoundResponse .Message ,
178179handler :func (t * testing.T ,ctx context.Context ) http.HandlerFunc {
179180return func (w http.ResponseWriter ,r * http.Request ) {
@@ -192,6 +193,40 @@ func TestTaskCreate(t *testing.T) {
192193}
193194},
194195},
196+ {
197+ args : []string {"template-in-different-org" ,"--input" ,"my-custom-prompt" ,"--org" ,anotherOrganizationID .String ()},
198+ expectError :httpapi .ResourceNotFoundResponse .Message ,
199+ handler :func (t * testing.T ,ctx context.Context ) http.HandlerFunc {
200+ return func (w http.ResponseWriter ,r * http.Request ) {
201+ switch r .URL .Path {
202+ case "/api/v2/users/me/organizations" :
203+ httpapi .Write (ctx ,w ,http .StatusOK , []codersdk.Organization {
204+ {MinimalOrganization : codersdk.MinimalOrganization {
205+ ID :anotherOrganizationID ,
206+ }},
207+ })
208+ case fmt .Sprintf ("/api/v2/organizations/%s/templates/template-in-different-org" ,anotherOrganizationID ):
209+ httpapi .ResourceNotFound (w )
210+ default :
211+ t .Errorf ("unexpected path: %s" ,r .URL .Path )
212+ }
213+ }
214+ },
215+ },
216+ {
217+ args : []string {"no-org" ,"--input" ,"my-custom-prompt" },
218+ expectError :"Must select an organization with --org=<org_name>" ,
219+ handler :func (t * testing.T ,ctx context.Context ) http.HandlerFunc {
220+ return func (w http.ResponseWriter ,r * http.Request ) {
221+ switch r .URL .Path {
222+ case "/api/v2/users/me/organizations" :
223+ httpapi .Write (ctx ,w ,http .StatusOK , []codersdk.Organization {})
224+ default :
225+ t .Errorf ("unexpected path: %s" ,r .URL .Path )
226+ }
227+ }
228+ },
229+ },
195230}
196231
197232for _ ,tt := range tests {