|
| 1 | +package migrations |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/shellhub-io/shellhub/pkg/envs" |
| 8 | + envmock "github.com/shellhub-io/shellhub/pkg/envs/mocks" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + migrate "github.com/xakep666/mongo-migrate" |
| 12 | + "go.mongodb.org/mongo-driver/bson" |
| 13 | + "go.mongodb.org/mongo-driver/bson/primitive" |
| 14 | +) |
| 15 | + |
| 16 | +func TestMigration89Up(t *testing.T) { |
| 17 | + ctx := context.Background() |
| 18 | + |
| 19 | + mock := &envmock.Backend{} |
| 20 | + envs.DefaultBackend = mock |
| 21 | + |
| 22 | + tests := []struct { |
| 23 | + description string |
| 24 | + setup func() error |
| 25 | + }{ |
| 26 | + { |
| 27 | + description: "Apply up on migration 89", |
| 28 | + setup: func() error { |
| 29 | + _, err := c. |
| 30 | + Database("test"). |
| 31 | + Collection("system"). |
| 32 | + InsertOne(ctx, map[string]interface{}{ |
| 33 | + "authentication": map[string]interface{}{ |
| 34 | + "local": map[string]interface{}{ |
| 35 | + "enabled": true, |
| 36 | + }, |
| 37 | + }, |
| 38 | + }) |
| 39 | + |
| 40 | + return err |
| 41 | + }, |
| 42 | + }, |
| 43 | + } |
| 44 | + |
| 45 | + for _, tc := range tests { |
| 46 | + t.Run(tc.description, func(tt *testing.T) { |
| 47 | + tt.Cleanup(func() { |
| 48 | + assert.NoError(tt, srv.Reset()) |
| 49 | + }) |
| 50 | + |
| 51 | + assert.NoError(tt, tc.setup()) |
| 52 | + |
| 53 | + migrates := migrate.NewMigrate(c.Database("test"), GenerateMigrations()[88]) |
| 54 | + require.NoError(tt, migrates.Up(context.Background(), migrate.AllAvailable)) |
| 55 | + |
| 56 | + query := c. |
| 57 | + Database("test"). |
| 58 | + Collection("system"). |
| 59 | + FindOne(context.TODO(), bson.M{}) |
| 60 | + |
| 61 | + system := make(map[string]interface{}) |
| 62 | + require.NoError(tt, query.Decode(&system)) |
| 63 | + |
| 64 | + saml, ok := system["authentication"].(map[string]interface{})["saml"].(map[string]interface{}) |
| 65 | + require.Equal(tt, true, ok) |
| 66 | + |
| 67 | + enabled, ok := saml["enabled"] |
| 68 | + require.Equal(tt, true, ok) |
| 69 | + require.Equal(tt, false, enabled) |
| 70 | + |
| 71 | + idp, ok := saml["idp"].(map[string]interface{}) |
| 72 | + require.Equal(tt, true, ok) |
| 73 | + require.Equal(tt, map[string]interface{}{"entity_id": "", "signon_url": "", "certificates": primitive.A{}}, idp) |
| 74 | + |
| 75 | + sp, ok := saml["sp"].(map[string]interface{}) |
| 76 | + require.Equal(tt, true, ok) |
| 77 | + require.Equal(tt, map[string]interface{}{"sign_auth_requests": false, "certificate": "", "private_key": ""}, sp) |
| 78 | + }) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func TestMigration89Down(t *testing.T) { |
| 83 | + ctx := context.Background() |
| 84 | + |
| 85 | + mock := &envmock.Backend{} |
| 86 | + envs.DefaultBackend = mock |
| 87 | + |
| 88 | + mock.On("Get", "SHELLHUB_CLOUD").Return("false") |
| 89 | + mock.On("Get", "SHELLHUB_ENTERPRISE").Return("false") |
| 90 | + |
| 91 | + tests := []struct { |
| 92 | + description string |
| 93 | + setup func() error |
| 94 | + }{ |
| 95 | + { |
| 96 | + description: "Apply up on migration 89", |
| 97 | + setup: func() error { |
| 98 | + _, err := c. |
| 99 | + Database("test"). |
| 100 | + Collection("system"). |
| 101 | + InsertOne(ctx, map[string]interface{}{ |
| 102 | + "authentication": map[string]interface{}{ |
| 103 | + "local": true, |
| 104 | + }, |
| 105 | + }) |
| 106 | + |
| 107 | + return err |
| 108 | + }, |
| 109 | + }, |
| 110 | + } |
| 111 | + |
| 112 | + for _, tc := range tests { |
| 113 | + t.Run(tc.description, func(tt *testing.T) { |
| 114 | + tt.Cleanup(func() { |
| 115 | + assert.NoError(tt, srv.Reset()) |
| 116 | + }) |
| 117 | + |
| 118 | + assert.NoError(tt, tc.setup()) |
| 119 | + |
| 120 | + migrates := migrate.NewMigrate(c.Database("test"), GenerateMigrations()[88]) |
| 121 | + require.NoError(tt, migrates.Up(context.Background(), migrate.AllAvailable)) |
| 122 | + require.NoError(tt, migrates.Down(context.Background(), migrate.AllAvailable)) |
| 123 | + |
| 124 | + query := c. |
| 125 | + Database("test"). |
| 126 | + Collection("system"). |
| 127 | + FindOne(context.TODO(), bson.M{}) |
| 128 | + |
| 129 | + system := make(map[string]interface{}) |
| 130 | + require.NoError(tt, query.Decode(&system)) |
| 131 | + |
| 132 | + _, ok := system["authentication"].(map[string]interface{})["saml"] |
| 133 | + require.Equal(tt, false, ok) |
| 134 | + }) |
| 135 | + } |
| 136 | +} |
0 commit comments