add some test for codeword repo
This commit is contained in:
parent
5b083eaee9
commit
68906556b8
@ -67,7 +67,7 @@ func (r *RecoveryController) HandleRecoveryRequest(c *fiber.Ctx) error {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Internal Server Error"})
|
||||
}
|
||||
|
||||
signUrl := req.RedirectionURL + base64.URLEncoding.EncodeToString(key)
|
||||
signUrl := req.RedirectionURL
|
||||
sign := base64.URLEncoding.EncodeToString(key)
|
||||
|
||||
id, err := r.service.StoreRecoveryRecord(c.Context(), models.StoreRecDeps{UserID: user.ID.Hex(), Email: user.Email, Key: sign, Url: signUrl})
|
||||
|
||||
@ -26,7 +26,7 @@ func NewUserRepository(deps Deps) *UserRepository {
|
||||
func (r *UserRepository) FindByEmail(ctx context.Context, email string) (*models.User, error) {
|
||||
var user models.User
|
||||
|
||||
err := r.mdb.FindOne(ctx, bson.M{"email": email}).Decode(&user)
|
||||
err := r.mdb.FindOne(ctx, bson.M{"login": email}).Decode(&user)
|
||||
if err != nil {
|
||||
if err == mongo.ErrNoDocuments {
|
||||
return nil, nil
|
||||
|
||||
@ -7,6 +7,7 @@ import (
|
||||
"fmt"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"log"
|
||||
@ -21,6 +22,7 @@ import (
|
||||
|
||||
const mongoURI = "mongodb://test:test@127.0.0.1:27020/?authMechanism=SCRAM-SHA-256&authSource=admin&directConnection=true"
|
||||
|
||||
// codeword unit tests
|
||||
func TestFindByEmail(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
@ -45,14 +47,15 @@ func TestFindByEmail(t *testing.T) {
|
||||
userRepo := repository.NewUserRepository(repository.Deps{Rdb: nil, Mdb: db.Collection("users")})
|
||||
|
||||
t.Run("FindByEmail - existing user", func(t *testing.T) {
|
||||
user, err := userRepo.FindByEmail(ctx, "email@mail.ru")
|
||||
user, err := userRepo.FindByEmail(ctx, "admin")
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, user)
|
||||
assert.Equal(t, "email@mail.ru", user.Email)
|
||||
fmt.Println(user.Email)
|
||||
assert.Equal(t, "admin", user.Login)
|
||||
})
|
||||
|
||||
t.Run("FindByEmail - non-existing user", func(t *testing.T) {
|
||||
user, err := userRepo.FindByEmail(ctx, "nonexisting@example.com")
|
||||
user, err := userRepo.FindByEmail(ctx, "neadmin")
|
||||
assert.NoError(t, err)
|
||||
assert.Nil(t, user)
|
||||
})
|
||||
@ -60,7 +63,8 @@ func TestFindByEmail(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestStoreRecoveryRecord(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
mongoClient, err := mongo.Connect(ctx, options.Client().ApplyURI(mongoURI))
|
||||
require.NoError(t, err)
|
||||
@ -93,3 +97,49 @@ func TestStoreRecoveryRecord(t *testing.T) {
|
||||
|
||||
_ = database.Drop(ctx)
|
||||
}
|
||||
|
||||
func TestGetRecoveryRecord(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
mongoClient, err := mongo.Connect(ctx, options.Client().ApplyURI(mongoURI))
|
||||
require.NoError(t, err)
|
||||
|
||||
defer func() {
|
||||
_ = mongoClient.Disconnect(ctx)
|
||||
}()
|
||||
|
||||
database := mongoClient.Database("admin")
|
||||
codeword := database.Collection("codeword")
|
||||
_ = codeword.Drop(ctx)
|
||||
|
||||
userRepo := repository.NewCodewordRepository(repository.Deps{Rdb: nil, Mdb: codeword})
|
||||
|
||||
ID := primitive.NewObjectID()
|
||||
userID := "6597babdd1ba7e2dbd32d7e3"
|
||||
email := "test@mail.ru"
|
||||
key := "test_recovery_key"
|
||||
|
||||
record := models.RestoreRequest{
|
||||
ID: ID,
|
||||
UserID: userID,
|
||||
Email: email,
|
||||
Sign: key,
|
||||
SignUrl: "def.url",
|
||||
SignID: key + userID,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
_, err = codeword.InsertOne(ctx, record)
|
||||
assert.NoError(t, err)
|
||||
|
||||
result, err := userRepo.GetRecoveryRecord(ctx, key+userID)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, userID, result.UserID)
|
||||
assert.Equal(t, email, result.Email)
|
||||
assert.Equal(t, key, result.Sign)
|
||||
|
||||
_ = database.Drop(ctx)
|
||||
}
|
||||
|
||||
// promocode unit tests
|
||||
|
||||
Loading…
Reference in New Issue
Block a user