Skip to content

Commit

Permalink
Remove gock related code
Browse files Browse the repository at this point in the history
Signed-off-by: Baiju Muthukadan <[email protected]>
  • Loading branch information
baijum committed Oct 12, 2023
1 parent 2393adf commit 795fc70
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 115 deletions.
115 changes: 1 addition & 114 deletions controllers/useraccount/useraccount_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/base64"
"errors"
"fmt"
"net/http"
"os"
"testing"
"time"
Expand All @@ -25,7 +24,6 @@ import (
"github.com/redhat-cop/operator-utils/pkg/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/h2non/gock.v1"
corev1 "k8s.io/api/core/v1"
apierros "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -40,11 +38,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

const (
testCheURL = "https://codeready-codeready-workspaces-operator.member-cluster"
testKeycloakURL = "https://keycloak-codeready-workspaces-operator.member-cluster"
)

func TestReconcile(t *testing.T) {
logf.SetLogger(zap.New(zap.UseDevMode(true)))
os.Setenv("WATCH_NAMESPACE", test.MemberOperatorNs)
Expand Down Expand Up @@ -421,16 +414,10 @@ func TestReconcile(t *testing.T) {

// given

// when the member operator secret exists and has a che admin user configured then che user deletion is enabled
// when
cfg := commonconfig.NewMemberOperatorConfigWithReset(t)

mockCallsCounter := new(int)
defer gock.OffAll()
gockTokenSuccess(mockCallsCounter)
gockFindUserTimes(username, 2, mockCallsCounter)
gockFindUserNoBody(404, mockCallsCounter)
gockDeleteUser(204, mockCallsCounter)

userAcc := newUserAccount(username, userID)
util.AddFinalizer(userAcc, toolchainv1alpha1.FinalizerName)
r, req, cl, _ := prepareReconcile(t, username, cfg, userAcc, preexistingUser, preexistingIdentity)
Expand Down Expand Up @@ -703,50 +690,6 @@ func TestReconcile(t *testing.T) {
})
})

// delete Che user fails
t.Run("delete che user fails because find che user request failed", func(t *testing.T) {
// given

// when the member operator secret exists and has a che admin user configured then che user deletion is enabled
cfg := commonconfig.NewMemberOperatorConfigWithReset(t)

mockCallsCounter := new(int)
defer gock.OffAll()
gockTokenSuccess(mockCallsCounter)
gockFindUserNoBody(400, mockCallsCounter) // respond with 400 error to simulate find user request failure

userAcc := newUserAccount(username, userID)
r, req, _, _ := prepareReconcile(t, username, cfg, userAcc, preexistingUser, preexistingIdentity)

// when
res, err := r.Reconcile(context.TODO(), req)
require.NoError(t, err)
assert.Equal(t, reconcile.Result{}, res)

// then
userAcc = useraccount.AssertThatUserAccount(t, username, r.Client).
HasFinalizer(toolchainv1alpha1.FinalizerName).
Get()

// Set the deletionTimestamp
now := metav1.NewTime(time.Now())
userAcc.DeletionTimestamp = &now
err = r.Client.Update(context.TODO(), userAcc)
require.NoError(t, err)

res, _ = r.Reconcile(context.TODO(), req)
assert.Equal(t, reconcile.Result{}, res)

// Check that the associated identity has not been deleted
// when reconciling the useraccount with a deletion timestamp
assertIdentity(t, r, userAcc, config.Auth().Idp())

// Check that the associated user has not been deleted
// when reconciling the useraccount with a deletion timestamp
assertUser(t, r, userAcc)
require.Equal(t, 0, *mockCallsCounter)
})

// delete identity fails
t.Run("delete identity fails", func(t *testing.T) {
// given
Expand Down Expand Up @@ -1743,59 +1686,3 @@ func terminating(msg string) toolchainv1alpha1.Condition {
Message: msg,
}
}

func gockTokenSuccess(calls *int) {
gock.New(testKeycloakURL).
Post("auth/realms/codeready/protocol/openid-connect/token").
SetMatcher(SpyOnGockCalls(calls)).
MatchHeader("Content-Type", "application/x-www-form-urlencoded").
Persist().
Reply(200).
BodyString(`{
"access_token":"abc.123.xyz",
"expires_in":300,
"refresh_expires_in":1800,
"refresh_token":"111.222.333",
"token_type":"bearer",
"not-before-policy":0,
"session_state":"a2fa1448-687a-414f-af40-3b6b3f5a873a",
"scope":"profile email"
}`)
}

func gockFindUserTimes(name string, times int, calls *int) { //nolint: unparam
gock.New(testCheURL).
Get("api/user/find").
SetMatcher(SpyOnGockCalls(calls)).
MatchHeader("Authorization", "Bearer abc.123.xyz").
Times(times).
Reply(200).
BodyString(fmt.Sprintf(`{"name":"%s","id":"abc1234"}`, name))
}

func gockFindUserNoBody(code int, calls *int) { //nolint: unparam
gock.New(testCheURL).
Get("api/user/find").
SetMatcher(SpyOnGockCalls(calls)).
MatchHeader("Authorization", "Bearer abc.123.xyz").
Persist().
Reply(code)
}

func gockDeleteUser(code int, calls *int) {
gock.New(testCheURL).
Delete("api/user").
SetMatcher(SpyOnGockCalls(calls)).
MatchHeader("Authorization", "Bearer abc.123.xyz").
Persist().
Reply(code)
}

func SpyOnGockCalls(counter *int) gock.Matcher {
matcher := gock.NewBasicMatcher()
matcher.Add(func(_ *http.Request, _ *gock.Request) (bool, error) {
*counter++
return true, nil
})
return matcher
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/redhat-cop/operator-utils v1.3.3-0.20220121120056-862ef22b8cdf
github.com/stretchr/testify v1.8.4
go.uber.org/zap v1.21.0
gopkg.in/h2non/gock.v1 v1.0.14
gopkg.in/h2non/gock.v1 v1.0.14 // indirect
k8s.io/api v0.25.0
k8s.io/client-go v0.25.0
k8s.io/klog v1.0.0
Expand Down

0 comments on commit 795fc70

Please sign in to comment.