Skip to content

Commit

Permalink
add first name and last name to get seats response (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
dagbay-rh authored Aug 17, 2023
1 parent 0213b58 commit 103bb99
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
2 changes: 1 addition & 1 deletion ams/test_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ var MockGetSubscriptions = func(organizationId string, size, page int) (*v1.Subs
lst, err := v1.NewSubscriptionList().
Items(
v1.NewSubscription().
Creator(v1.NewAccount().Username("testuser")).
Creator(v1.NewAccount().Username("testuser").FirstName("test").LastName("user")).
Plan(v1.NewPlan().Type("AnsibleWisdom").Name("AnsibleWisdom")).
Status("Active"),
).Build()
Expand Down
6 changes: 6 additions & 0 deletions apispec/api.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,12 @@
},
"status": {
"type": "string"
},
"first_name": {
"type": "string"
},
"last_name": {
"type": "string"
}
}
},
Expand Down
11 changes: 9 additions & 2 deletions controllers/ams.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func doError(w http.ResponseWriter, code int, err error) {
}

func do500(w http.ResponseWriter, err error) {
logger.Log.WithFields(logrus.Fields{"error": err}).Error("ams request error")
doError(w, http.StatusInternalServerError, err)
}

Expand Down Expand Up @@ -152,10 +151,18 @@ func (s *SeatManagerApi) GetSeats(w http.ResponseWriter, r *http.Request, params
}
subs.Each(func(sub *v1.Subscription) bool {
if _, exclude := excludeStatus[strings.ToLower(sub.Status())]; !exclude {
creator, ok := sub.GetCreator()
if !ok {
logger.Log.WithFields(logrus.Fields{"warning": fmt.Sprintf("Missing creator data for subscription [%s]", sub.ID())}).Warn("missing ams creator data")
creator, _ = v1.NewAccount().FirstName("UNKNOWN").LastName("UNKNOWN").Username("UNKNOWN").Build()
}

seats = append(seats, api.Seat{
AccountUsername: toPtr(sub.Creator().Username()),
AccountUsername: toPtr(creator.Username()),
SubscriptionId: toPtr(sub.ID()),
Status: toPtr(sub.Status()),
FirstName: toPtr(creator.FirstName()),
LastName: toPtr(creator.LastName()),
})
}
return true
Expand Down
33 changes: 31 additions & 2 deletions controllers/seat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/RedHatInsights/entitlements-api-go/bop"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
v1 "github.com/openshift-online/ocm-sdk-go/accountsmgmt/v1"
"github.com/redhatinsights/platform-go-middlewares/identity"
)

Expand Down Expand Up @@ -71,8 +72,6 @@ func MakeRequest(method, path string, body io.Reader, options ...opt) *http.Requ

}



var _ = Describe("using the seat managment api", func() {
var client ams.AMSInterface
var bopClient bop.Bop
Expand Down Expand Up @@ -130,6 +129,9 @@ var _ = Describe("using the seat managment api", func() {

Expect(*result.Meta.Count).To(Equal(int64(1)))
Expect(*result.Data[0].AccountUsername).To(Equal("testuser"))
Expect(*result.Data[0].FirstName).To(Equal("test"))
Expect(*result.Data[0].LastName).To(Equal("user"))
Expect(*result.Data[0].Status).To(Equal("Active"))

})
Context("and seats with active status is excluded", func() {
Expand Down Expand Up @@ -202,6 +204,33 @@ var _ = Describe("using the seat managment api", func() {
Expect(rr.Result().StatusCode).To(Equal(http.StatusBadRequest))
})
})
Context("and creator info is missing", func() {
It("should not fail and fill in missing data", func() {
ams.MockGetSubscriptions = func(organizationId string, size, page int) (*v1.SubscriptionList, error) {
lst, err := v1.NewSubscriptionList().
Items(
v1.NewSubscription().
Plan(v1.NewPlan().Type("AnsibleWisdom").Name("AnsibleWisdom")).
Status("Active"),
).Build()
if err != nil {
return nil, err
}
return lst, nil
}

req := MakeRequest("GET", "/api/entitlements/v1/seats", nil)
seatApi.GetSeats(rr, req, api.GetSeatsParams{})

var result api.ListSeatsResponsePagination
json.NewDecoder(rr.Result().Body).Decode(&result)

Expect(*result.Meta.Count).To(Equal(int64(1)))
Expect(*result.Data[0].AccountUsername).To(Equal("UNKNOWN"))
Expect(*result.Data[0].FirstName).To(Equal("UNKNOWN"))
Expect(*result.Data[0].LastName).To(Equal("UNKNOWN"))
})
})
})

When("adding a user to a seat", func() {
Expand Down

0 comments on commit 103bb99

Please sign in to comment.