-
-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): This commit is to add migration63
to include MFA fields in the users collection
- Loading branch information
Showing
14 changed files
with
132 additions
and
83 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
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
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package migrations | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/sirupsen/logrus" | ||
migrate "github.com/xakep666/mongo-migrate" | ||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
) | ||
|
||
var migration63 = migrate.Migration{ | ||
Version: 63, | ||
Description: "add MFA fields to collection users", | ||
Up: func(db *mongo.Database) error { | ||
logrus.WithFields(logrus.Fields{ | ||
"component": "migration", | ||
"version": 63, | ||
"action": "Up", | ||
}).Info("Applying migration") | ||
|
||
update := bson.M{ | ||
"$set": bson.M{ | ||
"status_mfa": false, | ||
"secret": "", | ||
"codes": []string{}, | ||
}, | ||
} | ||
|
||
if _, err := db.Collection("users").UpdateMany(context.TODO(), bson.M{}, update); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}, | ||
Down: func(db *mongo.Database) error { | ||
logrus.WithFields(logrus.Fields{ | ||
"component": "migration", | ||
"version": 63, | ||
"action": "Down", | ||
}).Info("Reverting migration") | ||
|
||
update := bson.M{ | ||
"$unset": bson.M{ | ||
"status_mfa": "", | ||
"secret": "", | ||
"codes": "", | ||
}, | ||
} | ||
|
||
if _, err := db.Collection("users").UpdateMany(context.TODO(), bson.M{}, update); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}, | ||
} |
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,55 @@ | ||
package migrations | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/shellhub-io/shellhub/api/pkg/dbtest" | ||
"github.com/shellhub-io/shellhub/pkg/models" | ||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/assert" | ||
migrate "github.com/xakep666/mongo-migrate" | ||
"go.mongodb.org/mongo-driver/bson" | ||
) | ||
|
||
func TestMigration63(t *testing.T) { | ||
logrus.Info("Testing Migration 63 - Test whether MFA fields were added to the users collection") | ||
|
||
db := dbtest.DBServer{} | ||
defer db.Stop() | ||
|
||
user := models.User{ | ||
UserData: models.UserData{ | ||
Name: "Test", | ||
}, | ||
} | ||
|
||
_, err := db.Client().Database("test").Collection("users").InsertOne(context.TODO(), user) | ||
assert.NoError(t, err) | ||
|
||
migrations := GenerateMigrations()[62:63] | ||
|
||
migrates := migrate.NewMigrate(db.Client().Database("test"), migrations...) | ||
err = migrates.Up(migrate.AllAvailable) | ||
assert.NoError(t, err) | ||
|
||
version, _, err := migrates.Version() | ||
assert.NoError(t, err) | ||
assert.Equal(t, uint64(63), version) | ||
|
||
var migratedUser *models.User | ||
err = db.Client().Database("test").Collection("users").FindOne(context.TODO(), bson.M{"name": user.Name}).Decode(&migratedUser) | ||
assert.NoError(t, err) | ||
assert.False(t, migratedUser.MFA) | ||
assert.Equal(t, "", migratedUser.Secret) | ||
assert.Empty(t, migratedUser.Codes) | ||
|
||
err = migrates.Down(migrate.AllAvailable) | ||
assert.NoError(t, err) | ||
|
||
err = db.Client().Database("test").Collection("users").FindOne(context.TODO(), bson.M{"name": user.Name}).Decode(&migratedUser) | ||
assert.NoError(t, err) | ||
assert.False(t, migratedUser.MFA) | ||
assert.Equal(t, "", migratedUser.Secret) | ||
assert.Empty(t, migratedUser.Codes) | ||
} |
Oops, something went wrong.