Skip to content

Commit

Permalink
fix: test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
aarmam committed Mar 30, 2022
1 parent 3946f69 commit 4eea405
Show file tree
Hide file tree
Showing 4 changed files with 897 additions and 11 deletions.
139 changes: 128 additions & 11 deletions consent/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ import (
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/pkg/errors"

"github.com/stretchr/testify/assert"
"github.com/tidwall/gjson"

Expand Down Expand Up @@ -238,7 +241,9 @@ func TestDeleteConsentSession(t *testing.T) {
cl := createClientWithBackChannelEndpoint(t, reg, "client-1", []string{"login-session-1"}, backChannelWG)
performLoginFlow(t, reg, "1", cl)
performLoginFlow(t, reg, "2", cl)
performDeleteConsentSession(t, reg, "client-1", "login-session-1", true)

performDeleteConsentSession(t, reg, "client-1", "login-session-1", true, false)

c1, err := reg.ConsentManager().GetConsentRequest(context.Background(), "consent-challenge-1")
require.Error(t, x.ErrNotFound, err)
require.Nil(t, c1)
Expand All @@ -255,7 +260,9 @@ func TestDeleteConsentSession(t *testing.T) {
cl := createClientWithBackChannelEndpoint(t, reg, "client-1", []string{}, backChannelWG)
performLoginFlow(t, reg, "1", cl)
performLoginFlow(t, reg, "2", cl)
performDeleteConsentSession(t, reg, "client-1", "login-session-1", false)

performDeleteConsentSession(t, reg, "client-1", "login-session-1", false, false)

c1, err := reg.ConsentManager().GetConsentRequest(context.Background(), "consent-challenge-1")
require.Error(t, x.ErrNotFound, err)
require.Nil(t, c1)
Expand All @@ -265,6 +272,32 @@ func TestDeleteConsentSession(t *testing.T) {
backChannelWG.Wait()
})

t.Run("case=subject=subject-1,client=client-1,session=session-1,expect revoke error", func(t *testing.T) {
conf := internal.NewConfigurationWithDefaults()
reg := internal.NewRegistryMemory(t, conf)
ctrl := gomock.NewController(t)
persister := reg.Persister()
mockPersister := NewMockManager(ctrl)
defer ctrl.Finish()
mockPersister.EXPECT().RevokeSubjectClientLoginSessionConsentSession(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(errors.New("SqlError"))
backChannelWG := newWg(0)
cl := createClientWithBackChannelEndpoint(t, reg, "client-1", []string{"login-session-1"}, backChannelWG)
performLoginFlow(t, reg, "1", cl)
performLoginFlow(t, reg, "2", cl)

reg.WithPersister(mockPersister)
performDeleteConsentSession(t, reg, "client-1", "login-session-1", false, true)
reg.WithPersister(persister)

c1, err := reg.ConsentManager().GetConsentRequest(context.Background(), "consent-challenge-1")
require.NoError(t, err)
require.NotNil(t, c1)
c2, err := reg.ConsentManager().GetConsentRequest(context.Background(), "consent-challenge-2")
require.NoError(t, err)
require.NotNil(t, c2)
backChannelWG.Wait()
})

t.Run("case=subject=subject-1,client=client-1,trigger_back_channel_logout=true", func(t *testing.T) {
conf := internal.NewConfigurationWithDefaults()
reg := internal.NewRegistryMemory(t, conf)
Expand All @@ -273,7 +306,7 @@ func TestDeleteConsentSession(t *testing.T) {
performLoginFlow(t, reg, "1", cl)
performLoginFlow(t, reg, "2", cl)

performDeleteConsentSession(t, reg, "client-1", nil, true)
performDeleteConsentSession(t, reg, "client-1", nil, true, false)

c1, err := reg.ConsentManager().GetConsentRequest(context.Background(), "consent-challenge-1")
require.Error(t, x.ErrNotFound, err)
Expand All @@ -292,7 +325,7 @@ func TestDeleteConsentSession(t *testing.T) {
performLoginFlow(t, reg, "1", cl)
performLoginFlow(t, reg, "2", cl)

performDeleteConsentSession(t, reg, "client-1", nil, false)
performDeleteConsentSession(t, reg, "client-1", nil, false, false)

c1, err := reg.ConsentManager().GetConsentRequest(context.Background(), "consent-challenge-1")
require.Error(t, x.ErrNotFound, err)
Expand All @@ -303,6 +336,32 @@ func TestDeleteConsentSession(t *testing.T) {
backChannelWG.Wait()
})

t.Run("case=subject=subject-1,client=client-1,expect revoke error", func(t *testing.T) {
conf := internal.NewConfigurationWithDefaults()
reg := internal.NewRegistryMemory(t, conf)
ctrl := gomock.NewController(t)
persister := reg.Persister()
mockPersister := NewMockManager(ctrl)
defer ctrl.Finish()
mockPersister.EXPECT().RevokeSubjectClientConsentSession(gomock.Any(), gomock.Any(), gomock.Any()).Return(errors.New("SqlError"))
backChannelWG := newWg(0)
cl := createClientWithBackChannelEndpoint(t, reg, "client-1", []string{"login-session-1"}, backChannelWG)
performLoginFlow(t, reg, "1", cl)
performLoginFlow(t, reg, "2", cl)

reg.WithPersister(mockPersister)
performDeleteConsentSession(t, reg, "client-1", nil, false, true)
reg.WithPersister(persister)

c1, err := reg.ConsentManager().GetConsentRequest(context.Background(), "consent-challenge-1")
require.NoError(t, err)
require.NotNil(t, c1)
c2, err := reg.ConsentManager().GetConsentRequest(context.Background(), "consent-challenge-2")
require.NoError(t, err)
require.NotNil(t, c2)
backChannelWG.Wait()
})

t.Run("case=subject=subject-1,all=true,session=session-1,trigger_back_channel_logout=true", func(t *testing.T) {
conf := internal.NewConfigurationWithDefaults()
reg := internal.NewRegistryMemory(t, conf)
Expand All @@ -312,7 +371,7 @@ func TestDeleteConsentSession(t *testing.T) {
performLoginFlow(t, reg, "1", cl1)
performLoginFlow(t, reg, "2", cl2)

performDeleteConsentSession(t, reg, nil, "login-session-1", true)
performDeleteConsentSession(t, reg, nil, "login-session-1", true, false)

c1, err := reg.ConsentManager().GetConsentRequest(context.Background(), "consent-challenge-1")
require.Error(t, x.ErrNotFound, err)
Expand All @@ -332,7 +391,7 @@ func TestDeleteConsentSession(t *testing.T) {
performLoginFlow(t, reg, "1", cl1)
performLoginFlow(t, reg, "2", cl2)

performDeleteConsentSession(t, reg, nil, "login-session-1", false)
performDeleteConsentSession(t, reg, nil, "login-session-1", false, false)

c1, err := reg.ConsentManager().GetConsentRequest(context.Background(), "consent-challenge-1")
require.Error(t, x.ErrNotFound, err)
Expand All @@ -343,6 +402,33 @@ func TestDeleteConsentSession(t *testing.T) {
backChannelWG.Wait()
})

t.Run("case=subject=subject-1,all=true,session=session-1,expect revoke error", func(t *testing.T) {
conf := internal.NewConfigurationWithDefaults()
reg := internal.NewRegistryMemory(t, conf)
ctrl := gomock.NewController(t)
persister := reg.Persister()
mockPersister := NewMockManager(ctrl)
defer ctrl.Finish()
mockPersister.EXPECT().RevokeLoginSessionConsentSession(gomock.Any(), gomock.Any()).Return(errors.New("SqlError"))
backChannelWG := newWg(0)
cl1 := createClientWithBackChannelEndpoint(t, reg, "client-1", []string{}, backChannelWG)
cl2 := createClientWithBackChannelEndpoint(t, reg, "client-2", []string{}, backChannelWG)
performLoginFlow(t, reg, "1", cl1)
performLoginFlow(t, reg, "2", cl2)

reg.WithPersister(mockPersister)
performDeleteConsentSession(t, reg, nil, "login-session-1", false, true)
reg.WithPersister(persister)

c1, err := reg.ConsentManager().GetConsentRequest(context.Background(), "consent-challenge-1")
require.NoError(t, err)
require.NotNil(t, c1)
c2, err := reg.ConsentManager().GetConsentRequest(context.Background(), "consent-challenge-2")
require.NoError(t, err)
require.NotNil(t, c2)
backChannelWG.Wait()
})

t.Run("case=subject=subject-1,all=true,trigger_back_channel_logout=true", func(t *testing.T) {
conf := internal.NewConfigurationWithDefaults()
reg := internal.NewRegistryMemory(t, conf)
Expand All @@ -352,7 +438,7 @@ func TestDeleteConsentSession(t *testing.T) {
performLoginFlow(t, reg, "1", cl1)
performLoginFlow(t, reg, "2", cl2)

performDeleteConsentSession(t, reg, nil, nil, true)
performDeleteConsentSession(t, reg, nil, nil, true, false)

c1, err := reg.ConsentManager().GetConsentRequest(context.Background(), "consent-challenge-1")
require.Error(t, x.ErrNotFound, err)
Expand All @@ -372,7 +458,7 @@ func TestDeleteConsentSession(t *testing.T) {
performLoginFlow(t, reg, "1", cl1)
performLoginFlow(t, reg, "2", cl2)

performDeleteConsentSession(t, reg, nil, nil, false)
performDeleteConsentSession(t, reg, nil, nil, false, false)

c1, err := reg.ConsentManager().GetConsentRequest(context.Background(), "consent-challenge-1")
require.Error(t, x.ErrNotFound, err)
Expand All @@ -382,9 +468,36 @@ func TestDeleteConsentSession(t *testing.T) {
require.Nil(t, c2)
backChannelWG.Wait()
})

t.Run("case=subject=subject-1,all=true,expect revoke error", func(t *testing.T) {
conf := internal.NewConfigurationWithDefaults()
reg := internal.NewRegistryMemory(t, conf)
ctrl := gomock.NewController(t)
persister := reg.Persister()
mockPersister := NewMockManager(ctrl)
defer ctrl.Finish()
mockPersister.EXPECT().RevokeSubjectConsentSession(gomock.Any(), gomock.Any()).Return(errors.New("SqlError"))
backChannelWG := newWg(0)
cl1 := createClientWithBackChannelEndpoint(t, reg, "client-1", []string{}, backChannelWG)
cl2 := createClientWithBackChannelEndpoint(t, reg, "client-2", []string{}, backChannelWG)
performLoginFlow(t, reg, "1", cl1)
performLoginFlow(t, reg, "2", cl2)

reg.WithPersister(mockPersister)
performDeleteConsentSession(t, reg, nil, nil, false, true)
reg.WithPersister(persister)

c1, err := reg.ConsentManager().GetConsentRequest(context.Background(), "consent-challenge-1")
require.NoError(t, err)
require.NotNil(t, c1)
c2, err := reg.ConsentManager().GetConsentRequest(context.Background(), "consent-challenge-2")
require.NoError(t, err)
require.NotNil(t, c2)
backChannelWG.Wait()
})
}

func performDeleteConsentSession(t *testing.T, reg driver.Registry, client, loginSessionId interface{}, triggerBackChannelLogout bool) {
func performDeleteConsentSession(t *testing.T, reg driver.Registry, client, loginSessionId interface{}, triggerBackChannelLogout bool, expectError bool) {
h := NewHandler(reg, reg.Config())
r := x.NewRouterAdmin()
h.SetRoutes(r)
Expand All @@ -410,8 +523,12 @@ func performDeleteConsentSession(t *testing.T, reg driver.Registry, client, logi
req, err := http.NewRequest(http.MethodDelete, u.String(), nil)

require.NoError(t, err)
_, err = c.Do(req)
require.NoError(t, err)
res, err := c.Do(req)
if expectError {
require.Equal(t, 500, res.StatusCode)
} else {
require.Equal(t, 204, res.StatusCode)
}
}

func performLoginFlow(t *testing.T, reg driver.Registry, flowId string, cl *client.Client) {
Expand Down
Loading

0 comments on commit 4eea405

Please sign in to comment.