@@ -29,27 +29,28 @@ func TestTaskCreate(t *testing.T) {
29
29
taskCreatedAt = time .Now ()
30
30
31
31
organizationID = uuid .New ()
32
+ anotherOrganizationID = uuid .New ()
32
33
templateID = uuid .New ()
33
34
templateVersionID = uuid .New ()
34
35
templateVersionPresetID = uuid .New ()
35
36
)
36
37
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 {
38
39
t .Helper ()
39
40
40
41
return func (w http.ResponseWriter , r * http.Request ) {
41
42
switch r .URL .Path {
42
43
case "/api/v2/users/me/organizations" :
43
44
httpapi .Write (ctx , w , http .StatusOK , []codersdk.Organization {
44
45
{MinimalOrganization : codersdk.MinimalOrganization {
45
- ID : organizationID ,
46
+ ID : orgID ,
46
47
}},
47
48
})
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 ):
49
50
httpapi .Write (ctx , w , http .StatusOK , codersdk.TemplateVersion {
50
51
ID : templateVersionID ,
51
52
})
52
- case fmt .Sprintf ("/api/v2/organizations/%s/templates/my-template" , organizationID ):
53
+ case fmt .Sprintf ("/api/v2/organizations/%s/templates/my-template" , orgID ):
53
54
httpapi .Write (ctx , w , http .StatusOK , codersdk.Template {
54
55
ID : templateID ,
55
56
ActiveVersionID : templateVersionID ,
@@ -94,62 +95,62 @@ func TestTaskCreate(t *testing.T) {
94
95
handler func (t * testing.T , ctx context.Context ) http.HandlerFunc
95
96
}{
96
97
{
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 () },
98
99
expectOutput : fmt .Sprintf ("The task %s has been created at %s!" , cliui .Keyword ("task-wild-goldfish-27" ), cliui .Timestamp (taskCreatedAt )),
99
100
handler : 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" )
101
102
},
102
103
},
103
104
{
104
- args : []string {"my-template" , "--input" , "my custom prompt" },
105
+ args : []string {"my-template" , "--input" , "my custom prompt" , "--org" , organizationID . String () },
105
106
env : []string {"CODER_TASK_TEMPLATE_VERSION=my-template-version" },
106
107
expectOutput : fmt .Sprintf ("The task %s has been created at %s!" , cliui .Keyword ("task-wild-goldfish-27" ), cliui .Timestamp (taskCreatedAt )),
107
108
handler : 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" )
109
110
},
110
111
},
111
112
{
112
- args : []string {"--input" , "my custom prompt" },
113
+ args : []string {"--input" , "my custom prompt" , "--org" , organizationID . String () },
113
114
env : []string {"CODER_TASK_TEMPLATE_NAME=my-template" , "CODER_TASK_TEMPLATE_VERSION=my-template-version" },
114
115
expectOutput : fmt .Sprintf ("The task %s has been created at %s!" , cliui .Keyword ("task-wild-goldfish-27" ), cliui .Timestamp (taskCreatedAt )),
115
116
handler : 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" )
117
118
},
118
119
},
119
120
{
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 () },
121
122
expectOutput : fmt .Sprintf ("The task %s has been created at %s!" , cliui .Keyword ("task-wild-goldfish-27" ), cliui .Timestamp (taskCreatedAt )),
122
123
handler : 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" )
124
125
},
125
126
},
126
127
{
127
- args : []string {"my-template" , "--input" , "my custom prompt" },
128
+ args : []string {"my-template" , "--input" , "my custom prompt" , "--org" , organizationID . String () },
128
129
expectOutput : fmt .Sprintf ("The task %s has been created at %s!" , cliui .Keyword ("task-wild-goldfish-27" ), cliui .Timestamp (taskCreatedAt )),
129
130
handler : 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" )
131
132
},
132
133
},
133
134
{
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 () },
135
136
expectOutput : fmt .Sprintf ("The task %s has been created at %s!" , cliui .Keyword ("task-wild-goldfish-27" ), cliui .Timestamp (taskCreatedAt )),
136
137
handler : 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" )
138
139
},
139
140
},
140
141
{
141
142
args : []string {"my-template" , "--input" , "my custom prompt" },
142
143
env : []string {"CODER_TASK_PRESET_NAME=my-preset" },
143
144
expectOutput : fmt .Sprintf ("The task %s has been created at %s!" , cliui .Keyword ("task-wild-goldfish-27" ), cliui .Timestamp (taskCreatedAt )),
144
145
handler : 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" )
146
147
},
147
148
},
148
149
{
149
150
args : []string {"my-template" , "--input" , "my custom prompt" , "--preset" , "not-real-preset" },
150
151
expectError : `preset "not-real-preset" not found` ,
151
152
handler : 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" )
153
154
},
154
155
},
155
156
{
@@ -173,7 +174,7 @@ func TestTaskCreate(t *testing.T) {
173
174
},
174
175
},
175
176
{
176
- args : []string {"not-real-template" , "--input" , "my custom prompt" },
177
+ args : []string {"not-real-template" , "--input" , "my custom prompt" , "--org" , organizationID . String () },
177
178
expectError : httpapi .ResourceNotFoundResponse .Message ,
178
179
handler : func (t * testing.T , ctx context.Context ) http.HandlerFunc {
179
180
return func (w http.ResponseWriter , r * http.Request ) {
@@ -192,6 +193,40 @@ func TestTaskCreate(t *testing.T) {
192
193
}
193
194
},
194
195
},
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
+ },
195
230
}
196
231
197
232
for _ , tt := range tests {
0 commit comments