Skip to content

Commit d77a9f6

Browse files
committed
RHINENG-15506: fix test for candlepin API
1 parent 754bd15 commit d77a9f6

5 files changed

Lines changed: 34 additions & 33 deletions

File tree

base/database/testing.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -540,9 +540,7 @@ func CheckTemplateSystems(t *testing.T, account int, templateUUID string, invent
540540
assert.Nil(t, err)
541541

542542
assert.Equal(t, len(inventoryIDs), len(dbInventoryIDs))
543-
if len(inventoryIDs) == 0 {
544-
assert.Equal(t, 0, len(dbInventoryIDs))
545-
} else {
543+
if len(inventoryIDs) == len(dbInventoryIDs) {
546544
for index, inventoryID := range inventoryIDs {
547545
assert.Equal(t, inventoryID, dbInventoryIDs[index])
548546
}

manager/controllers/template_subscribed_systems_update_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
var subscriptionUUID = "cccccccc-0000-0000-0001-000000000004"
1616
var templateSystemUUID = "00000000-0000-0000-0000-000000000004"
1717
var subscriptionInvalidUUID = "99999999-9999-8888-8888-888888888888"
18+
var orgID = "org_1"
1819

1920
func TestSubscribedSystemID(t *testing.T) {
2021
core.SetupTest(t)
@@ -49,7 +50,8 @@ func TestUpdateTemplateSubscribedSystems(t *testing.T) {
4950

5051
w := CreateRequestRouterWithParams("PATCH", "/:template_id/subscribed-systems", templateUUID, "", nil, "",
5152
TemplateSubscribedSystemsUpdateHandler, templateAccount,
52-
core.ContextKV{Key: utils.KeySystem, Value: subscriptionUUID})
53+
core.ContextKV{Key: utils.KeySystem, Value: subscriptionUUID},
54+
core.ContextKV{Key: utils.KeyOrgID, Value: orgID})
5355

5456
assert.Equal(t, http.StatusOK, w.Code)
5557
database.CheckTemplateSystems(t, templateAccount, templateUUID, []string{templateSystemUUID})

manager/controllers/template_systems_update.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,9 @@ func assignCandlepinEnvironment(c *gin.Context, db *gorm.DB, accountID int, env
253253
resp, err := callCandlepin(base.Context, c.GetString(utils.KeyOrgID), &updateReq)
254254
// check response
255255
if err != nil {
256+
if resp == nil {
257+
resp = &candlepin.ConsumersUpdateResponse{Message: "call to candlepin failed"}
258+
}
256259
LogAndRespBadRequest(c, err, resp.Message)
257260
return err
258261
}

manager/controllers/template_systems_update_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,22 +226,22 @@ func TestUpdateTemplateSystemsCandlepin404(t *testing.T) {
226226
})
227227
defer database.DeleteTemplate(t, templateAccount, templateUUID)
228228
w := CreateRequestRouterWithParams("PUT", templatePath, templateUUID, "", bytes.NewBufferString(data), "",
229-
TemplateSystemsUpdateHandler, templateAccount)
229+
TemplateSystemsUpdateHandler, templateAccount, core.ContextKV{Key: utils.KeyOrgID, Value: orgID})
230230

231-
assert.Equal(t, http.StatusFailedDependency, w.Code)
231+
assert.Equal(t, http.StatusBadRequest, w.Code)
232232
database.CheckTemplateSystems(t, templateAccount, templateUUID, []string{"00000000-0000-0000-0000-000000000007"})
233233

234-
// Expect HTTP 200 status code when only 1 system causes candlepin call error
234+
// Expect HTTP 400 status code even when only 1 system causes candlepin call error
235235
data = `{
236236
"systems": [
237237
"00000000-0000-0000-0000-000000000004",
238238
"00000000-0000-0000-0000-000000000018"
239239
]
240240
}`
241241
w = CreateRequestRouterWithParams("PUT", templatePath, templateUUID, "", bytes.NewBufferString(data), "",
242-
TemplateSystemsUpdateHandler, templateAccount)
242+
TemplateSystemsUpdateHandler, templateAccount, core.ContextKV{Key: utils.KeyOrgID, Value: orgID})
243243

244-
assert.Equal(t, http.StatusOK, w.Code)
244+
assert.Equal(t, http.StatusBadRequest, w.Code)
245245
database.CheckTemplateSystems(t, templateAccount, templateUUID,
246-
[]string{"00000000-0000-0000-0000-000000000004", "00000000-0000-0000-0000-000000000007"})
246+
[]string{"00000000-0000-0000-0000-000000000007"})
247247
}

platform/candlepin.go

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,15 @@ package platform
33
import (
44
"app/base/candlepin"
55
"app/base/utils"
6-
"fmt"
6+
"encoding/json"
77
"io"
88
"net/http"
9+
"slices"
910
"strings"
1011

1112
"github.com/gin-gonic/gin"
1213
)
1314

14-
func candlepinEnvHandler(c *gin.Context) {
15-
envID := c.Param("envid")
16-
/*
17-
jsonData, _ := io.ReadAll(c.Request.Body)
18-
json.Unmarshal(jsonData, &body) // nolint:errcheck
19-
if body.ReturnStatus > 200 {
20-
c.AbortWithStatus(body.ReturnStatus)
21-
return
22-
}
23-
*/
24-
data := fmt.Sprintf(`{
25-
"environment": "%s"
26-
}`, envID)
27-
utils.LogInfo(data)
28-
if envID == "return_404" {
29-
c.Data(http.StatusNotFound, gin.MIMEJSON, []byte{})
30-
return
31-
}
32-
c.Data(http.StatusOK, gin.MIMEJSON, []byte(data))
33-
}
34-
3515
func candlepinConsumersPutHandler(c *gin.Context) {
3616
consumer := c.Param("consumer")
3717
jsonData, _ := io.ReadAll(c.Request.Body)
@@ -56,8 +36,26 @@ func candlepinConsumersGetHandler(c *gin.Context) {
5636
c.JSON(http.StatusOK, response)
5737
}
5838

39+
func candlepinConsumersEnvironmentsHandler(c *gin.Context) {
40+
owner := c.Param("owner")
41+
jsonData, _ := io.ReadAll(c.Request.Body)
42+
utils.LogInfo("owner", owner, "body", string(jsonData))
43+
var req candlepin.ConsumersEnvironmentsRequest
44+
err := json.Unmarshal(jsonData, &req)
45+
if err != nil {
46+
c.Data(http.StatusInternalServerError, gin.MIMEJSON, []byte{})
47+
return
48+
}
49+
utils.LogInfo("ConsumerUuids", req.ConsumerUuids)
50+
if slices.Contains(req.ConsumerUuids, "return_404") {
51+
c.Data(http.StatusNotFound, gin.MIMEJSON, []byte{})
52+
return
53+
}
54+
c.Data(http.StatusOK, gin.MIMEJSON, []byte{})
55+
}
56+
5957
func initCandlepin(app *gin.Engine) {
60-
app.POST("/candlepin/environments/:envid/consumers", candlepinEnvHandler)
6158
app.PUT("/candlepin/consumers/:consumer", candlepinConsumersPutHandler)
6259
app.GET("/candlepin/consumers/:consumer", candlepinConsumersGetHandler)
60+
app.PUT("/candlepin/owner/:owner/consumers/environments", candlepinConsumersEnvironmentsHandler)
6361
}

0 commit comments

Comments
 (0)