2021-05-20 11:33:35 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-10-19 10:34:00 +00:00
|
|
|
"github.com/zitadel/oidc/v3/pkg/oidc"
|
2021-05-20 11:33:35 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2021-05-20 11:33:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCommands_RevokeRefreshToken(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
eventstore *eventstore.Eventstore
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
userID string
|
|
|
|
orgID string
|
|
|
|
tokenID string
|
|
|
|
}
|
|
|
|
type res struct {
|
|
|
|
want *domain.ObjectDetails
|
|
|
|
err func(error) bool
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"missing param, error",
|
|
|
|
fields{
|
|
|
|
eventstore: eventstoreExpect(t),
|
|
|
|
},
|
|
|
|
args{},
|
|
|
|
res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsErrorInvalidArgument,
|
2021-05-20 11:33:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"token not active, error",
|
|
|
|
fields{
|
|
|
|
eventstore: eventstoreExpect(t,
|
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args{
|
|
|
|
context.Background(),
|
|
|
|
"userID",
|
|
|
|
"orgID",
|
|
|
|
"tokenID",
|
|
|
|
},
|
|
|
|
res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsNotFound,
|
2021-05-20 11:33:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"push failed, error",
|
|
|
|
fields{
|
|
|
|
eventstore: eventstoreExpect(t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(user.NewHumanRefreshTokenAddedEvent(
|
|
|
|
context.Background(),
|
|
|
|
&user.NewAggregate("userID", "orgID").Aggregate,
|
|
|
|
"tokenID",
|
|
|
|
"clientID",
|
|
|
|
"agentID",
|
|
|
|
"de",
|
|
|
|
[]string{"clientID1"},
|
|
|
|
[]string{oidc.ScopeOpenID, oidc.ScopeProfile, oidc.ScopeEmail, oidc.ScopeOfflineAccess},
|
|
|
|
[]string{"password"},
|
|
|
|
time.Now(),
|
|
|
|
1*time.Hour,
|
|
|
|
10*time.Hour,
|
2024-03-20 10:18:46 +00:00
|
|
|
nil,
|
2021-05-20 11:33:35 +00:00
|
|
|
)),
|
|
|
|
),
|
2023-12-08 14:30:55 +00:00
|
|
|
expectPushFailed(zerrors.ThrowInternal(nil, "ERROR", "internal"),
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanRefreshTokenRemovedEvent(
|
|
|
|
context.Background(),
|
|
|
|
&user.NewAggregate("userID", "orgID").Aggregate,
|
|
|
|
"tokenID",
|
|
|
|
),
|
2021-05-20 11:33:35 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args{
|
|
|
|
context.Background(),
|
|
|
|
"userID",
|
|
|
|
"orgID",
|
|
|
|
"tokenID",
|
|
|
|
},
|
|
|
|
res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsInternal,
|
2021-05-20 11:33:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"revoke, ok",
|
|
|
|
fields{
|
|
|
|
eventstore: eventstoreExpect(t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(user.NewHumanRefreshTokenAddedEvent(
|
|
|
|
context.Background(),
|
|
|
|
&user.NewAggregate("userID", "orgID").Aggregate,
|
|
|
|
"tokenID",
|
|
|
|
"clientID",
|
|
|
|
"agentID",
|
|
|
|
"de",
|
|
|
|
[]string{"clientID1"},
|
|
|
|
[]string{oidc.ScopeOpenID, oidc.ScopeProfile, oidc.ScopeEmail, oidc.ScopeOfflineAccess},
|
|
|
|
[]string{"password"},
|
|
|
|
time.Now(),
|
|
|
|
1*time.Hour,
|
|
|
|
10*time.Hour,
|
2024-03-20 10:18:46 +00:00
|
|
|
nil,
|
2021-05-20 11:33:35 +00:00
|
|
|
)),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanRefreshTokenRemovedEvent(
|
|
|
|
context.Background(),
|
|
|
|
&user.NewAggregate("userID", "orgID").Aggregate,
|
|
|
|
"tokenID",
|
|
|
|
),
|
2021-05-20 11:33:35 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args{
|
|
|
|
context.Background(),
|
|
|
|
"userID",
|
|
|
|
"orgID",
|
|
|
|
"tokenID",
|
|
|
|
},
|
|
|
|
res{
|
|
|
|
want: &domain.ObjectDetails{
|
|
|
|
ResourceOwner: "orgID",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := &Commands{
|
|
|
|
eventstore: tt.fields.eventstore,
|
|
|
|
}
|
|
|
|
got, err := c.RevokeRefreshToken(tt.args.ctx, tt.args.userID, tt.args.orgID, tt.args.tokenID)
|
|
|
|
if tt.res.err == nil {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
if tt.res.err != nil && !tt.res.err(err) {
|
|
|
|
t.Errorf("got wrong err: %v ", err)
|
|
|
|
}
|
|
|
|
if tt.res.err == nil {
|
2024-08-12 20:32:01 +00:00
|
|
|
assertObjectDetails(t, tt.res.want, got)
|
2021-05-20 11:33:35 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommands_RevokeRefreshTokens(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
eventstore *eventstore.Eventstore
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
userID string
|
|
|
|
orgID string
|
|
|
|
tokenIDs []string
|
|
|
|
}
|
|
|
|
type res struct {
|
|
|
|
err func(error) bool
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"missing tokenIDs, error",
|
|
|
|
fields{
|
|
|
|
eventstore: eventstoreExpect(t),
|
|
|
|
},
|
|
|
|
args{
|
|
|
|
context.Background(),
|
|
|
|
"userID",
|
|
|
|
"orgID",
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsErrorInvalidArgument,
|
2021-05-20 11:33:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"one token not active, error",
|
|
|
|
fields{
|
|
|
|
eventstore: eventstoreExpect(t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(user.NewHumanRefreshTokenAddedEvent(
|
|
|
|
context.Background(),
|
|
|
|
&user.NewAggregate("userID", "orgID").Aggregate,
|
|
|
|
"tokenID",
|
|
|
|
"clientID",
|
|
|
|
"agentID",
|
|
|
|
"de",
|
|
|
|
[]string{"clientID1"},
|
|
|
|
[]string{oidc.ScopeOpenID, oidc.ScopeProfile, oidc.ScopeEmail, oidc.ScopeOfflineAccess},
|
|
|
|
[]string{"password"},
|
|
|
|
time.Now(),
|
|
|
|
1*time.Hour,
|
|
|
|
10*time.Hour,
|
2024-03-20 10:18:46 +00:00
|
|
|
nil,
|
2021-05-20 11:33:35 +00:00
|
|
|
)),
|
|
|
|
),
|
|
|
|
expectFilter(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args{
|
|
|
|
context.Background(),
|
|
|
|
"userID",
|
|
|
|
"orgID",
|
|
|
|
[]string{"tokenID", "tokenID2"},
|
|
|
|
},
|
|
|
|
res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsNotFound,
|
2021-05-20 11:33:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"push failed, error",
|
|
|
|
fields{
|
|
|
|
eventstore: eventstoreExpect(t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(user.NewHumanRefreshTokenAddedEvent(
|
|
|
|
context.Background(),
|
|
|
|
&user.NewAggregate("userID", "orgID").Aggregate,
|
|
|
|
"tokenID",
|
|
|
|
"clientID",
|
|
|
|
"agentID",
|
|
|
|
"de",
|
|
|
|
[]string{"clientID"},
|
|
|
|
[]string{oidc.ScopeOpenID, oidc.ScopeProfile, oidc.ScopeEmail, oidc.ScopeOfflineAccess},
|
|
|
|
[]string{"password"},
|
|
|
|
time.Now(),
|
|
|
|
1*time.Hour,
|
|
|
|
10*time.Hour,
|
2024-03-20 10:18:46 +00:00
|
|
|
nil,
|
2021-05-20 11:33:35 +00:00
|
|
|
)),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(user.NewHumanRefreshTokenAddedEvent(
|
|
|
|
context.Background(),
|
|
|
|
&user.NewAggregate("userID", "orgID").Aggregate,
|
|
|
|
"tokenID2",
|
|
|
|
"clientID2",
|
|
|
|
"agentID",
|
|
|
|
"de",
|
|
|
|
[]string{"clientID2"},
|
|
|
|
[]string{oidc.ScopeOpenID, oidc.ScopeProfile, oidc.ScopeEmail, oidc.ScopeOfflineAccess},
|
|
|
|
[]string{"password"},
|
|
|
|
time.Now(),
|
|
|
|
1*time.Hour,
|
|
|
|
10*time.Hour,
|
2024-03-20 10:18:46 +00:00
|
|
|
nil,
|
2021-05-20 11:33:35 +00:00
|
|
|
)),
|
|
|
|
),
|
2023-12-08 14:30:55 +00:00
|
|
|
expectPushFailed(zerrors.ThrowInternal(nil, "ERROR", "internal"),
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanRefreshTokenRemovedEvent(
|
|
|
|
context.Background(),
|
|
|
|
&user.NewAggregate("userID", "orgID").Aggregate,
|
|
|
|
"tokenID",
|
|
|
|
),
|
|
|
|
user.NewHumanRefreshTokenRemovedEvent(
|
|
|
|
context.Background(),
|
|
|
|
&user.NewAggregate("userID", "orgID").Aggregate,
|
|
|
|
"tokenID2",
|
|
|
|
),
|
2021-05-20 11:33:35 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args{
|
|
|
|
context.Background(),
|
|
|
|
"userID",
|
|
|
|
"orgID",
|
|
|
|
[]string{"tokenID", "tokenID2"},
|
|
|
|
},
|
|
|
|
res{
|
2023-12-08 14:30:55 +00:00
|
|
|
err: zerrors.IsInternal,
|
2021-05-20 11:33:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"revoke, ok",
|
|
|
|
fields{
|
|
|
|
eventstore: eventstoreExpect(t,
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(user.NewHumanRefreshTokenAddedEvent(
|
|
|
|
context.Background(),
|
|
|
|
&user.NewAggregate("userID", "orgID").Aggregate,
|
|
|
|
"tokenID",
|
|
|
|
"clientID",
|
|
|
|
"agentID",
|
|
|
|
"de",
|
|
|
|
[]string{"clientID1"},
|
|
|
|
[]string{oidc.ScopeOpenID, oidc.ScopeProfile, oidc.ScopeEmail, oidc.ScopeOfflineAccess},
|
|
|
|
[]string{"password"},
|
|
|
|
time.Now(),
|
|
|
|
1*time.Hour,
|
|
|
|
10*time.Hour,
|
2024-03-20 10:18:46 +00:00
|
|
|
nil,
|
2021-05-20 11:33:35 +00:00
|
|
|
)),
|
|
|
|
),
|
|
|
|
expectFilter(
|
|
|
|
eventFromEventPusher(user.NewHumanRefreshTokenAddedEvent(
|
|
|
|
context.Background(),
|
|
|
|
&user.NewAggregate("userID", "orgID").Aggregate,
|
|
|
|
"tokenID2",
|
|
|
|
"clientID2",
|
|
|
|
"agentID",
|
|
|
|
"de",
|
|
|
|
[]string{"clientID2"},
|
|
|
|
[]string{oidc.ScopeOpenID, oidc.ScopeProfile, oidc.ScopeEmail, oidc.ScopeOfflineAccess},
|
|
|
|
[]string{"password"},
|
|
|
|
time.Now(),
|
|
|
|
1*time.Hour,
|
|
|
|
10*time.Hour,
|
2024-03-20 10:18:46 +00:00
|
|
|
nil,
|
2021-05-20 11:33:35 +00:00
|
|
|
)),
|
|
|
|
),
|
|
|
|
expectPush(
|
2023-10-19 10:19:10 +00:00
|
|
|
user.NewHumanRefreshTokenRemovedEvent(
|
|
|
|
context.Background(),
|
|
|
|
&user.NewAggregate("userID", "orgID").Aggregate,
|
|
|
|
"tokenID",
|
|
|
|
),
|
|
|
|
user.NewHumanRefreshTokenRemovedEvent(
|
|
|
|
context.Background(),
|
|
|
|
&user.NewAggregate("userID", "orgID").Aggregate,
|
|
|
|
"tokenID2",
|
|
|
|
),
|
2021-05-20 11:33:35 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
args{
|
|
|
|
context.Background(),
|
|
|
|
"userID",
|
|
|
|
"orgID",
|
|
|
|
[]string{"tokenID", "tokenID2"},
|
|
|
|
},
|
|
|
|
res{
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
c := &Commands{
|
|
|
|
eventstore: tt.fields.eventstore,
|
|
|
|
}
|
|
|
|
err := c.RevokeRefreshTokens(tt.args.ctx, tt.args.userID, tt.args.orgID, tt.args.tokenIDs)
|
|
|
|
if tt.res.err == nil {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
if tt.res.err != nil && !tt.res.err(err) {
|
|
|
|
t.Errorf("got wrong err: %v ", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|