Skip to content

Commit

Permalink
Added check endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
psav committed Sep 6, 2023
1 parent a2fd9d7 commit 31bb6d8
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 6 deletions.
21 changes: 21 additions & 0 deletions internal/service/catchall/ephemeral.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"strings"

"github.com/redhatinsights/mbop/internal/models"
"github.com/redhatinsights/mbop/internal/store"
"golang.org/x/oauth2/clientcredentials"
)

Expand Down Expand Up @@ -500,6 +501,24 @@ 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) {
cnValue := r.Header.Get("x-rh-check-reg")
if cnValue == "" {
http.Error(w, "could not get CN from header", http.StatusForbidden)
return
}

db := store.GetStore()

_, err := db.FindByUID(cnValue)
if err != nil {
http.Error(w, "cn not registered in db", http.StatusForbidden)
} else {
w.Write([]byte("gooo"))
return
}
}

func (m *MBOPServer) MainHandler(w http.ResponseWriter, r *http.Request) {
switch {
case r.URL.Path == "/v1/users":
Expand All @@ -516,6 +535,8 @@ func (m *MBOPServer) MainHandler(w http.ResponseWriter, r *http.Request) {
m.usersV2V3Handler(w, r)
case r.URL.Path == "/api/entitlements/v1/services":
m.entitlements(w, r)
case r.URL.Path == "/v1/check_registration":
m.check_registration(w, r)
}
}

Expand Down
79 changes: 73 additions & 6 deletions internal/service/catchall/ephemeral_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,41 @@ import (
"net/http/httptest"
"os"
"testing"
"time"

"github.com/redhatinsights/mbop/internal/config"
"github.com/redhatinsights/mbop/internal/logger"
"github.com/redhatinsights/mbop/internal/store"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

type TestSuite struct {
type RegistrationTestSuite struct {
suite.Suite
rec *httptest.ResponseRecorder
store store.Store
}

func (suite *TestSuite) SetupSuite() {
func (suite *RegistrationTestSuite) SetupSuite() {
_ = logger.Init()
config.Reset()
os.Setenv("STORE_BACKEND", "memory")
}

func (suite *TestSuite) TestJWTGet() {
func (suite *RegistrationTestSuite) BeforeTest(_, _ string) {
suite.rec = httptest.NewRecorder()
suite.Nil(store.SetupStore())

// creating a new store for every test and overriding the dep injection function
suite.store = store.GetStore()
store.GetStore = func() store.Store { return suite.store }
}

func (suite *RegistrationTestSuite) AfterTest(_, _ string) {
suite.rec.Result().Body.Close()
}

func (suite *RegistrationTestSuite) TestJWTGet() {
testData, _ := os.ReadFile("testdata/jwt.json")
testDataStruct := &JSONStruct{}
err := json.Unmarshal([]byte(testData), testDataStruct)
Expand Down Expand Up @@ -55,15 +77,60 @@ func (suite *TestSuite) TestJWTGet() {
defer resp.Body.Close()
}

func (suite *TestSuite) TestGetUrl() {
func (suite *RegistrationTestSuite) TestGetUrl() {
os.Setenv("KEYCLOAK_SERVER", "http://test")
path := MakeNewMBOPServer().getURL("path", map[string]string{"hi": "you"})
assert.Equal(suite.T(), "http://test/path?hi=you", path, "did not match")
}

func (suite *TestSuite) TearDownSuite() {
func (suite *RegistrationTestSuite) TearDownSuite() {
}

func TestExampleTestSuite(t *testing.T) {
suite.Run(t, new(TestSuite))
suite.Run(t, new(RegistrationTestSuite))
}

func (suite *RegistrationTestSuite) TestGoodRegistration() {
store.SetupStore()
db := store.GetStore()
db.Create(&store.Registration{
ID: "nark",
OrgID: "12345",
Username: "nark",
UID: "nark",
DisplayName: "foobar",
Extra: map[string]interface{}{},
CreatedAt: time.Time{},
})

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.Header.Set("x-rh-check-reg", "nark")
resp, err := http.DefaultClient.Do(req2)
suite.Nil(err)

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

func (suite *RegistrationTestSuite) TestBadRegistration() {

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.Header.Set("x-rh-check-reg", "nark")
resp, err := http.DefaultClient.Do(req2)
suite.Nil(err)

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

0 comments on commit 31bb6d8

Please sign in to comment.