-
-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Membership invitations in cloud instances will now send an invitation email to the user instead of automatically accepting the invite. Enterprise and community instances remain unchanged. Additionally, a new location has been added in the gateway to redirect acceptance requests to the cloud API.
- Loading branch information
1 parent
721b88b
commit c8a5eac
Showing
9 changed files
with
385 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,13 +6,16 @@ import ( | |
"strconv" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/shellhub-io/shellhub/api/store" | ||
"github.com/shellhub-io/shellhub/api/store/mocks" | ||
"github.com/shellhub-io/shellhub/pkg/api/authorizer" | ||
"github.com/shellhub-io/shellhub/pkg/api/query" | ||
"github.com/shellhub-io/shellhub/pkg/api/requests" | ||
storecache "github.com/shellhub-io/shellhub/pkg/cache" | ||
"github.com/shellhub-io/shellhub/pkg/clock" | ||
clockmocks "github.com/shellhub-io/shellhub/pkg/clock/mocks" | ||
"github.com/shellhub-io/shellhub/pkg/models" | ||
"github.com/shellhub-io/shellhub/pkg/uuid" | ||
uuid_mocks "github.com/shellhub-io/shellhub/pkg/uuid/mocks" | ||
|
@@ -1017,6 +1020,11 @@ func TestAddNamespaceMember(t *testing.T) { | |
} | ||
|
||
storeMock := new(mocks.Store) | ||
clockmock := new(clockmocks.Clock) | ||
clock.DefaultBackend = clockmock | ||
|
||
now := time.Now() | ||
clockmock.On("Now").Return(now) | ||
|
||
cases := []struct { | ||
description string | ||
|
@@ -1027,10 +1035,11 @@ func TestAddNamespaceMember(t *testing.T) { | |
{ | ||
description: "fails when the namespace was not found", | ||
req: &requests.NamespaceAddMember{ | ||
UserID: "000000000000000000000000", | ||
TenantID: "00000000-0000-4000-0000-000000000000", | ||
MemberEmail: "[email protected]", | ||
MemberRole: authorizer.RoleObserver, | ||
FowardedHost: "localhost", | ||
UserID: "000000000000000000000000", | ||
TenantID: "00000000-0000-4000-0000-000000000000", | ||
MemberEmail: "[email protected]", | ||
MemberRole: authorizer.RoleObserver, | ||
}, | ||
requiredMocks: func(ctx context.Context) { | ||
storeMock. | ||
|
@@ -1046,10 +1055,11 @@ func TestAddNamespaceMember(t *testing.T) { | |
{ | ||
description: "fails when the active member was not found", | ||
req: &requests.NamespaceAddMember{ | ||
UserID: "000000000000000000000000", | ||
TenantID: "00000000-0000-4000-0000-000000000000", | ||
MemberEmail: "[email protected]", | ||
MemberRole: authorizer.RoleObserver, | ||
FowardedHost: "localhost", | ||
UserID: "000000000000000000000000", | ||
TenantID: "00000000-0000-4000-0000-000000000000", | ||
MemberEmail: "[email protected]", | ||
MemberRole: authorizer.RoleObserver, | ||
}, | ||
requiredMocks: func(ctx context.Context) { | ||
storeMock. | ||
|
@@ -1074,10 +1084,11 @@ func TestAddNamespaceMember(t *testing.T) { | |
{ | ||
description: "fails when the active member is not on the namespace", | ||
req: &requests.NamespaceAddMember{ | ||
UserID: "000000000000000000000000", | ||
TenantID: "00000000-0000-4000-0000-000000000000", | ||
MemberEmail: "[email protected]", | ||
MemberRole: authorizer.RoleObserver, | ||
FowardedHost: "localhost", | ||
UserID: "000000000000000000000000", | ||
TenantID: "00000000-0000-4000-0000-000000000000", | ||
MemberEmail: "[email protected]", | ||
MemberRole: authorizer.RoleObserver, | ||
}, | ||
requiredMocks: func(ctx context.Context) { | ||
storeMock. | ||
|
@@ -1105,10 +1116,11 @@ func TestAddNamespaceMember(t *testing.T) { | |
{ | ||
description: "fails when the passive role's is owner", | ||
req: &requests.NamespaceAddMember{ | ||
UserID: "000000000000000000000000", | ||
TenantID: "00000000-0000-4000-0000-000000000000", | ||
MemberEmail: "[email protected]", | ||
MemberRole: authorizer.RoleOwner, | ||
FowardedHost: "localhost", | ||
UserID: "000000000000000000000000", | ||
TenantID: "00000000-0000-4000-0000-000000000000", | ||
MemberEmail: "[email protected]", | ||
MemberRole: authorizer.RoleOwner, | ||
}, | ||
requiredMocks: func(ctx context.Context) { | ||
storeMock. | ||
|
@@ -1142,10 +1154,11 @@ func TestAddNamespaceMember(t *testing.T) { | |
{ | ||
description: "fails when the active member's role cannot act over passive member's role", | ||
req: &requests.NamespaceAddMember{ | ||
UserID: "000000000000000000000000", | ||
TenantID: "00000000-0000-4000-0000-000000000000", | ||
MemberEmail: "[email protected]", | ||
MemberRole: authorizer.RoleAdministrator, | ||
FowardedHost: "localhost", | ||
UserID: "000000000000000000000000", | ||
TenantID: "00000000-0000-4000-0000-000000000000", | ||
MemberEmail: "[email protected]", | ||
MemberRole: authorizer.RoleAdministrator, | ||
}, | ||
requiredMocks: func(ctx context.Context) { | ||
storeMock. | ||
|
@@ -1179,10 +1192,11 @@ func TestAddNamespaceMember(t *testing.T) { | |
{ | ||
description: "fails when passive member was not found", | ||
req: &requests.NamespaceAddMember{ | ||
UserID: "000000000000000000000000", | ||
TenantID: "00000000-0000-4000-0000-000000000000", | ||
MemberEmail: "[email protected]", | ||
MemberRole: authorizer.RoleObserver, | ||
FowardedHost: "localhost", | ||
UserID: "000000000000000000000000", | ||
TenantID: "00000000-0000-4000-0000-000000000000", | ||
MemberEmail: "[email protected]", | ||
MemberRole: authorizer.RoleObserver, | ||
}, | ||
requiredMocks: func(ctx context.Context) { | ||
storeMock. | ||
|
@@ -1220,10 +1234,11 @@ func TestAddNamespaceMember(t *testing.T) { | |
{ | ||
description: "fails when cannot add the member", | ||
req: &requests.NamespaceAddMember{ | ||
UserID: "000000000000000000000000", | ||
TenantID: "00000000-0000-4000-0000-000000000000", | ||
MemberEmail: "[email protected]", | ||
MemberRole: authorizer.RoleObserver, | ||
FowardedHost: "localhost", | ||
UserID: "000000000000000000000000", | ||
TenantID: "00000000-0000-4000-0000-000000000000", | ||
MemberEmail: "[email protected]", | ||
MemberRole: authorizer.RoleObserver, | ||
}, | ||
requiredMocks: func(ctx context.Context) { | ||
storeMock. | ||
|
@@ -1255,6 +1270,10 @@ func TestAddNamespaceMember(t *testing.T) { | |
UserData: models.UserData{Username: "john_doe"}, | ||
}, nil). | ||
Once() | ||
envMock. | ||
On("Get", "SHELLHUB_CLOUD"). | ||
Return("false"). | ||
Once() | ||
storeMock. | ||
On("NamespaceAddMember", ctx, "00000000-0000-4000-0000-000000000000", &models.Member{ID: "000000000000000000000001", Role: authorizer.RoleObserver, Status: models.MemberStatusAccepted, AddedAt: now}). | ||
Return(errors.New("error")). | ||
|
@@ -1268,10 +1287,11 @@ func TestAddNamespaceMember(t *testing.T) { | |
{ | ||
description: "succeeds", | ||
req: &requests.NamespaceAddMember{ | ||
UserID: "000000000000000000000000", | ||
TenantID: "00000000-0000-4000-0000-000000000000", | ||
MemberEmail: "[email protected]", | ||
MemberRole: authorizer.RoleObserver, | ||
FowardedHost: "localhost", | ||
UserID: "000000000000000000000000", | ||
TenantID: "00000000-0000-4000-0000-000000000000", | ||
MemberEmail: "[email protected]", | ||
MemberRole: authorizer.RoleObserver, | ||
}, | ||
requiredMocks: func(ctx context.Context) { | ||
storeMock. | ||
|
@@ -1303,6 +1323,10 @@ func TestAddNamespaceMember(t *testing.T) { | |
UserData: models.UserData{Username: "john_doe"}, | ||
}, nil). | ||
Once() | ||
envMock. | ||
On("Get", "SHELLHUB_CLOUD"). | ||
Return("false"). | ||
Once() | ||
storeMock. | ||
On("NamespaceAddMember", ctx, "00000000-0000-4000-0000-000000000000", &models.Member{ID: "000000000000000000000001", Role: authorizer.RoleObserver, Status: models.MemberStatusAccepted, AddedAt: now}). | ||
Return(nil). | ||
|
@@ -1349,6 +1373,148 @@ func TestAddNamespaceMember(t *testing.T) { | |
err: nil, | ||
}, | ||
}, | ||
{ | ||
description: "fails when instance is cloud and cannot send the invite", | ||
req: &requests.NamespaceAddMember{ | ||
FowardedHost: "localhost", | ||
UserID: "000000000000000000000000", | ||
TenantID: "00000000-0000-4000-0000-000000000000", | ||
MemberEmail: "[email protected]", | ||
MemberRole: authorizer.RoleObserver, | ||
}, | ||
requiredMocks: func(ctx context.Context) { | ||
storeMock. | ||
On("NamespaceGet", ctx, "00000000-0000-4000-0000-000000000000", true). | ||
Return(&models.Namespace{ | ||
TenantID: "00000000-0000-4000-0000-000000000000", | ||
Name: "namespace", | ||
Owner: "000000000000000000000000", | ||
Members: []models.Member{ | ||
{ | ||
ID: "000000000000000000000000", | ||
Role: authorizer.RoleOwner, | ||
Status: models.MemberStatusAccepted, | ||
}, | ||
}, | ||
}, nil). | ||
Once() | ||
storeMock. | ||
On("UserGetByID", ctx, "000000000000000000000000", false). | ||
Return(&models.User{ | ||
ID: "000000000000000000000000", | ||
UserData: models.UserData{Username: "jane_doe"}, | ||
}, 0, nil). | ||
Once() | ||
storeMock. | ||
On("UserGetByEmail", ctx, "[email protected]"). | ||
Return(&models.User{ID: "000000000000000000000001"}, nil). | ||
Once() | ||
envMock. | ||
On("Get", "SHELLHUB_CLOUD"). | ||
Return("true"). | ||
Once() | ||
clientMock. | ||
On("InviteMember", ctx, "00000000-0000-4000-0000-000000000000", "000000000000000000000001", "localhost"). | ||
Return(errors.New("error")). | ||
Once() | ||
}, | ||
expected: Expected{ | ||
namespace: nil, | ||
err: errors.New("error"), | ||
}, | ||
}, | ||
{ | ||
description: "succeeds when the instance is cloud", | ||
req: &requests.NamespaceAddMember{ | ||
UserID: "000000000000000000000000", | ||
TenantID: "00000000-0000-4000-0000-000000000000", | ||
MemberEmail: "[email protected]", | ||
MemberRole: authorizer.RoleObserver, | ||
FowardedHost: "localhost", | ||
}, | ||
requiredMocks: func(ctx context.Context) { | ||
storeMock. | ||
On("NamespaceGet", ctx, "00000000-0000-4000-0000-000000000000", true). | ||
Return(&models.Namespace{ | ||
TenantID: "00000000-0000-4000-0000-000000000000", | ||
Name: "namespace", | ||
Owner: "000000000000000000000000", | ||
Members: []models.Member{ | ||
{ | ||
ID: "000000000000000000000000", | ||
Role: authorizer.RoleOwner, | ||
Status: models.MemberStatusAccepted, | ||
}, | ||
}, | ||
}, nil). | ||
Once() | ||
storeMock. | ||
On("UserGetByID", ctx, "000000000000000000000000", false). | ||
Return(&models.User{ | ||
ID: "000000000000000000000000", | ||
UserData: models.UserData{Username: "jane_doe"}, | ||
}, 0, nil). | ||
Once() | ||
storeMock. | ||
On("UserGetByEmail", ctx, "[email protected]"). | ||
Return(&models.User{ | ||
ID: "000000000000000000000001", | ||
}, nil). | ||
Once() | ||
envMock. | ||
On("Get", "SHELLHUB_CLOUD"). | ||
Return("true"). | ||
Once() | ||
clientMock. | ||
On("InviteMember", ctx, "00000000-0000-4000-0000-000000000000", "000000000000000000000001", "localhost"). | ||
Return(nil). | ||
Once() | ||
storeMock. | ||
On("NamespaceAddMember", ctx, "00000000-0000-4000-0000-000000000000", &models.Member{ID: "000000000000000000000001", Role: authorizer.RoleObserver, Status: models.MemberStatusPending, AddedAt: now}). | ||
Return(nil). | ||
Once() | ||
storeMock. | ||
On("NamespaceGet", ctx, "00000000-0000-4000-0000-000000000000", true). | ||
Return(&models.Namespace{ | ||
TenantID: "00000000-0000-4000-0000-000000000000", | ||
Name: "namespace", | ||
Owner: "000000000000000000000000", | ||
Members: []models.Member{ | ||
{ | ||
ID: "000000000000000000000000", | ||
Role: authorizer.RoleOwner, | ||
Status: models.MemberStatusAccepted, | ||
}, | ||
{ | ||
ID: "000000000000000000000001", | ||
Role: authorizer.RoleObserver, | ||
Status: models.MemberStatusPending, | ||
}, | ||
}, | ||
}, nil). | ||
Once() | ||
}, | ||
expected: Expected{ | ||
namespace: &models.Namespace{ | ||
TenantID: "00000000-0000-4000-0000-000000000000", | ||
Name: "namespace", | ||
Owner: "000000000000000000000000", | ||
Members: []models.Member{ | ||
{ | ||
ID: "000000000000000000000000", | ||
Role: authorizer.RoleOwner, | ||
Status: models.MemberStatusAccepted, | ||
}, | ||
{ | ||
ID: "000000000000000000000001", | ||
Role: authorizer.RoleObserver, | ||
Status: models.MemberStatusPending, | ||
}, | ||
}, | ||
}, | ||
err: nil, | ||
}, | ||
}, | ||
} | ||
|
||
s := NewService(store.Store(storeMock), privateKey, publicKey, storecache.NewNullCache(), clientMock) | ||
|
Oops, something went wrong.