Skip to content

Commit

Permalink
Fixed linting
Browse files Browse the repository at this point in the history
  • Loading branch information
psav committed Sep 11, 2023
1 parent a64c37f commit 211e36e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
15 changes: 11 additions & 4 deletions internal/service/catchall/ephemeral.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,20 +501,27 @@ func (m *MBOPServer) entitlements(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, string(userObj.Entitlements))
}

func (m *MBOPServer) check_registration(w http.ResponseWriter, r *http.Request) {
func (m *MBOPServer) checkRegistration(w http.ResponseWriter, r *http.Request) {
cnValue := r.Header.Get("x-rh-check-reg")
fmt.Printf("%s", cnValue)
if cnValue == "" {
http.Error(w, "could not get CN from header", http.StatusForbidden)
return
}

db := store.GetStore()

_, err := db.FindByUID(cnValue)
reg, err := db.FindByUID(cnValue)
if err != nil {
http.Error(w, "cn not registered in db", http.StatusForbidden)
} else {
return
str, err := json.Marshal(reg)
if err != nil {
http.Error(w, "could not create response for reg", http.StatusInternalServerError)
return
}

fmt.Fprint(w, string(str))
}
}

Expand All @@ -535,7 +542,7 @@ func (m *MBOPServer) MainHandler(w http.ResponseWriter, r *http.Request) {
case r.URL.Path == "/api/entitlements/v1/services":
m.entitlements(w, r)
case r.URL.Path == "/v1/check_registration":
m.check_registration(w, r)
m.checkRegistration(w, r)
}
}

Expand Down
29 changes: 24 additions & 5 deletions internal/service/catchall/ephemeral_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ func TestExampleTestSuite(t *testing.T) {
}

func (suite *RegistrationTestSuite) TestGoodRegistration() {
store.SetupStore()
suite.Nil(store.SetupStore())
db := store.GetStore()
db.Create(&store.Registration{
_, err := db.Create(&store.Registration{
ID: "nark",
OrgID: "12345",
Username: "nark",
Expand All @@ -103,33 +103,52 @@ func (suite *RegistrationTestSuite) TestGoodRegistration() {
CreatedAt: time.Time{},
})

suite.Nil(err)

mux := http.NewServeMux()
mux.Handle("/", http.HandlerFunc(MakeNewMBOPServer().MainHandler))

sut := httptest.NewServer(mux)
defer sut.Close()

req2, _ := http.NewRequest("GET", fmt.Sprintf("%s/v1/check_registration", sut.URL), nil)
req2, err := http.NewRequest("GET", fmt.Sprintf("%s/v1/check_registration", sut.URL), nil)
suite.Nil(err)

req2.Header.Set("x-rh-check-reg", "nark")
resp, err := http.DefaultClient.Do(req2)
suite.NoError(err)

dta, err := io.ReadAll(resp.Body)

suite.NoError(err)

resp.Body.Close()
suite.Nil(err)

obj := &store.Registration{}
json.Unmarshal(dta, obj)

suite.Equal("12345", obj.OrgID)

suite.Equal(http.StatusOK, resp.StatusCode)
}

func (suite *RegistrationTestSuite) TestBadRegistration() {

store.SetupStore()
suite.Nil(store.SetupStore())

mux := http.NewServeMux()
mux.Handle("/", http.HandlerFunc(MakeNewMBOPServer().MainHandler))

sut := httptest.NewServer(mux)
defer sut.Close()

req2, _ := http.NewRequest("GET", fmt.Sprintf("%s/v1/check_registration", sut.URL), nil)
req2, err := http.NewRequest("GET", fmt.Sprintf("%s/v1/check_registration", sut.URL), nil)
suite.Nil(err)

req2.Header.Set("x-rh-check-reg", "nark")
resp, err := http.DefaultClient.Do(req2)
resp.Body.Close()
suite.Nil(err)

suite.Equal(http.StatusForbidden, resp.StatusCode)
Expand Down

0 comments on commit 211e36e

Please sign in to comment.