Skip to content

Commit 8b3f46d

Browse files
committed
Add unit test cases
1 parent 0bb51b4 commit 8b3f46d

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

coderd/httpmw/workspaceparam.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,12 @@ func ExtractWorkspaceAndAgentParam(db database.Store) func(http.Handler) http.Ha
128128
return
129129
}
130130

131-
// If we have more than 1 workspace agent, we need to specify which one to use.
132131
var agent database.WorkspaceAgent
133132
var found bool
134-
if len(agents) > 1 {
133+
// If we have more than 1 workspace agent, we need to specify which one to use.
134+
// If the user specified an agent, we need to make sure that agent
135+
// actually exists.
136+
if len(workspaceParts) > 1 || len(agents) > 1 {
135137
for _, otherAgent := range agents {
136138
if otherAgent.Name == workspaceParts[1] {
137139
agent = otherAgent
@@ -141,7 +143,7 @@ func ExtractWorkspaceAndAgentParam(db database.Store) func(http.Handler) http.Ha
141143
}
142144
if !found {
143145
httpapi.Write(rw, http.StatusBadRequest, codersdk.Response{
144-
Message: fmt.Sprintf("No agent exists with the name %s", workspaceParts[1]),
146+
Message: fmt.Sprintf("No agent exists with the name %q", workspaceParts[1]),
145147
})
146148
return
147149
}

coderd/httpmw/workspaceparam_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,36 @@ func TestWorkspaceAgentByNameParam(t *testing.T) {
175175
ExpectedStatusCode: http.StatusBadRequest,
176176
ExpectedError: "More than one agent exists, but no agent specified",
177177
},
178+
{
179+
Name: "NotExistsOneAgent",
180+
WorkspaceName: "dev",
181+
Agents: map[string][]string{
182+
"resource-a": {
183+
"agent-one",
184+
},
185+
},
186+
UrlParam: "dev.not-exists",
187+
ExpectedStatusCode: http.StatusBadRequest,
188+
ExpectedError: "No agent exists with the name",
189+
},
190+
{
191+
Name: "NotExistsMultipleAgents",
192+
WorkspaceName: "dev",
193+
Agents: map[string][]string{
194+
"resource-a": {
195+
"agent-one",
196+
},
197+
"resource-b": {
198+
"agent-two",
199+
},
200+
"resource-c": {
201+
"agent-three",
202+
},
203+
},
204+
UrlParam: "dev.not-exists",
205+
ExpectedStatusCode: http.StatusBadRequest,
206+
ExpectedError: "No agent exists with the name",
207+
},
178208

179209
// OKs
180210
{

0 commit comments

Comments
 (0)