-
-
Notifications
You must be signed in to change notification settings - Fork 535
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/new-networks-concept' into feature/new-networks…
…-concept-updatePeers-hook
- Loading branch information
Showing
12 changed files
with
938 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
package networks | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/netbirdio/netbird/management/server/networks/types" | ||
"github.com/netbirdio/netbird/management/server/permissions" | ||
"github.com/netbirdio/netbird/management/server/store" | ||
) | ||
|
||
func Test_GetAllNetworksReturnsNetworks(t *testing.T) { | ||
ctx := context.Background() | ||
accountID := "testAccountId" | ||
userID := "allowedUser" | ||
|
||
s, cleanUp, err := store.NewTestStoreFromSQL(context.Background(), "../testdata/networks.sql", t.TempDir()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
t.Cleanup(cleanUp) | ||
permissionsManager := permissions.NewManagerMock() | ||
manager := NewManager(s, permissionsManager) | ||
|
||
networks, err := manager.GetAllNetworks(ctx, accountID, userID) | ||
require.NoError(t, err) | ||
require.Len(t, networks, 1) | ||
require.Equal(t, "testNetworkId", networks[0].ID) | ||
} | ||
|
||
func Test_GetAllNetworksReturnsPermissionDenied(t *testing.T) { | ||
ctx := context.Background() | ||
accountID := "testAccountId" | ||
userID := "invalidUser" | ||
|
||
s, cleanUp, err := store.NewTestStoreFromSQL(context.Background(), "../testdata/networks.sql", t.TempDir()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
t.Cleanup(cleanUp) | ||
permissionsManager := permissions.NewManagerMock() | ||
manager := NewManager(s, permissionsManager) | ||
|
||
networks, err := manager.GetAllNetworks(ctx, accountID, userID) | ||
require.Error(t, err) | ||
require.Nil(t, networks) | ||
} | ||
|
||
func Test_GetNetworkReturnsNetwork(t *testing.T) { | ||
ctx := context.Background() | ||
accountID := "testAccountId" | ||
userID := "allowedUser" | ||
networkID := "testNetworkId" | ||
|
||
s, cleanUp, err := store.NewTestStoreFromSQL(context.Background(), "../testdata/networks.sql", t.TempDir()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
t.Cleanup(cleanUp) | ||
permissionsManager := permissions.NewManagerMock() | ||
manager := NewManager(s, permissionsManager) | ||
|
||
networks, err := manager.GetNetwork(ctx, accountID, userID, networkID) | ||
require.NoError(t, err) | ||
require.Equal(t, "testNetworkId", networks.ID) | ||
} | ||
|
||
func Test_GetNetworkReturnsPermissionDenied(t *testing.T) { | ||
ctx := context.Background() | ||
accountID := "testAccountId" | ||
userID := "invalidUser" | ||
networkID := "testNetworkId" | ||
|
||
s, cleanUp, err := store.NewTestStoreFromSQL(context.Background(), "../testdata/networks.sql", t.TempDir()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
t.Cleanup(cleanUp) | ||
permissionsManager := permissions.NewManagerMock() | ||
manager := NewManager(s, permissionsManager) | ||
|
||
network, err := manager.GetNetwork(ctx, accountID, userID, networkID) | ||
require.Error(t, err) | ||
require.Nil(t, network) | ||
} | ||
|
||
func Test_CreateNetworkSuccessfully(t *testing.T) { | ||
ctx := context.Background() | ||
userID := "allowedUser" | ||
network := &types.Network{ | ||
AccountID: "testAccountId", | ||
Name: "new-network", | ||
} | ||
|
||
s, cleanUp, err := store.NewTestStoreFromSQL(context.Background(), "../testdata/networks.sql", t.TempDir()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
t.Cleanup(cleanUp) | ||
permissionsManager := permissions.NewManagerMock() | ||
manager := NewManager(s, permissionsManager) | ||
|
||
createdNetwork, err := manager.CreateNetwork(ctx, userID, network) | ||
require.NoError(t, err) | ||
require.Equal(t, network.Name, createdNetwork.Name) | ||
} | ||
|
||
func Test_CreateNetworkFailsWithPermissionDenied(t *testing.T) { | ||
ctx := context.Background() | ||
userID := "invalidUser" | ||
network := &types.Network{ | ||
AccountID: "testAccountId", | ||
Name: "new-network", | ||
} | ||
|
||
s, cleanUp, err := store.NewTestStoreFromSQL(context.Background(), "../testdata/networks.sql", t.TempDir()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
t.Cleanup(cleanUp) | ||
permissionsManager := permissions.NewManagerMock() | ||
manager := NewManager(s, permissionsManager) | ||
|
||
createdNetwork, err := manager.CreateNetwork(ctx, userID, network) | ||
require.Error(t, err) | ||
require.Nil(t, createdNetwork) | ||
} | ||
|
||
func Test_DeleteNetworkSuccessfully(t *testing.T) { | ||
ctx := context.Background() | ||
accountID := "testAccountId" | ||
userID := "allowedUser" | ||
networkID := "testNetworkId" | ||
|
||
s, cleanUp, err := store.NewTestStoreFromSQL(context.Background(), "../testdata/networks.sql", t.TempDir()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
t.Cleanup(cleanUp) | ||
permissionsManager := permissions.NewManagerMock() | ||
manager := NewManager(s, permissionsManager) | ||
|
||
err = manager.DeleteNetwork(ctx, accountID, userID, networkID) | ||
require.NoError(t, err) | ||
} | ||
|
||
func Test_DeleteNetworkFailsWithPermissionDenied(t *testing.T) { | ||
ctx := context.Background() | ||
accountID := "testAccountId" | ||
userID := "invalidUser" | ||
networkID := "testNetworkId" | ||
|
||
s, cleanUp, err := store.NewTestStoreFromSQL(context.Background(), "../testdata/networks.sql", t.TempDir()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
t.Cleanup(cleanUp) | ||
permissionsManager := permissions.NewManagerMock() | ||
manager := NewManager(s, permissionsManager) | ||
|
||
err = manager.DeleteNetwork(ctx, accountID, userID, networkID) | ||
require.Error(t, err) | ||
} | ||
|
||
func Test_UpdateNetworkSuccessfully(t *testing.T) { | ||
ctx := context.Background() | ||
userID := "allowedUser" | ||
network := &types.Network{ | ||
AccountID: "testAccountId", | ||
ID: "testNetworkId", | ||
Name: "new-network", | ||
} | ||
|
||
s, cleanUp, err := store.NewTestStoreFromSQL(context.Background(), "../testdata/networks.sql", t.TempDir()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
t.Cleanup(cleanUp) | ||
permissionsManager := permissions.NewManagerMock() | ||
manager := NewManager(s, permissionsManager) | ||
|
||
updatedNetwork, err := manager.UpdateNetwork(ctx, userID, network) | ||
require.NoError(t, err) | ||
require.Equal(t, network.Name, updatedNetwork.Name) | ||
} | ||
|
||
func Test_UpdateNetworkFailsWithPermissionDenied(t *testing.T) { | ||
ctx := context.Background() | ||
userID := "invalidUser" | ||
network := &types.Network{ | ||
AccountID: "testAccountId", | ||
ID: "testNetworkId", | ||
Name: "new-network", | ||
} | ||
|
||
s, cleanUp, err := store.NewTestStoreFromSQL(context.Background(), "../testdata/networks.sql", t.TempDir()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
t.Cleanup(cleanUp) | ||
permissionsManager := permissions.NewManagerMock() | ||
manager := NewManager(s, permissionsManager) | ||
|
||
updatedNetwork, err := manager.UpdateNetwork(ctx, userID, network) | ||
require.Error(t, err) | ||
require.Nil(t, updatedNetwork) | ||
} |
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
Oops, something went wrong.