Skip to content

Commit

Permalink
fix: test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
aarmam committed May 11, 2022
1 parent d877507 commit e6b1579
Show file tree
Hide file tree
Showing 4 changed files with 898 additions and 11 deletions.
140 changes: 129 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 @@ -225,6 +228,7 @@ func TestGetConsentRequest(t *testing.T) {
}

func TestDeleteConsentSession(t *testing.T) {

newWg := func(add int) *sync.WaitGroup {
var wg sync.WaitGroup
wg.Add(add)
Expand All @@ -238,7 +242,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 +261,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 +273,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 +307,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 +326,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 +337,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 +372,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 +392,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 +403,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 +439,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 +459,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 +469,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 +524,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 e6b1579

Please sign in to comment.