|
| 1 | +package apis |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "database/sql" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/gofiber/fiber/v2" |
| 12 | + "github.com/opentreehole/go-common" |
| 13 | + "gorm.io/driver/sqlite" |
| 14 | + "gorm.io/gorm" |
| 15 | + "gorm.io/gorm/schema" |
| 16 | + |
| 17 | + "auth_next/config" |
| 18 | + "auth_next/models" |
| 19 | + "auth_next/utils/auth" |
| 20 | +) |
| 21 | + |
| 22 | +func TestChangePasswordRejectsInactiveUser(t *testing.T) { |
| 23 | + oldDB := models.DB |
| 24 | + oldStandalone := config.Config.Standalone |
| 25 | + oldShamirFeature := config.Config.ShamirFeature |
| 26 | + oldEmailWhitelist := config.Config.EmailWhitelist |
| 27 | + oldVerificationCodeExpires := config.Config.VerificationCodeExpires |
| 28 | + t.Cleanup(func() { |
| 29 | + models.DB = oldDB |
| 30 | + config.Config.Standalone = oldStandalone |
| 31 | + config.Config.ShamirFeature = oldShamirFeature |
| 32 | + config.Config.EmailWhitelist = oldEmailWhitelist |
| 33 | + config.Config.VerificationCodeExpires = oldVerificationCodeExpires |
| 34 | + }) |
| 35 | + |
| 36 | + db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{ |
| 37 | + NamingStrategy: schema.NamingStrategy{SingularTable: true}, |
| 38 | + }) |
| 39 | + if err != nil { |
| 40 | + t.Fatal(err) |
| 41 | + } |
| 42 | + sqlDB, err := db.DB() |
| 43 | + if err != nil { |
| 44 | + t.Fatal(err) |
| 45 | + } |
| 46 | + t.Cleanup(func() { |
| 47 | + _ = sqlDB.Close() |
| 48 | + }) |
| 49 | + if err := db.AutoMigrate(models.User{}, models.DeleteIdentifier{}, models.UserJwtSecret{}); err != nil { |
| 50 | + t.Fatal(err) |
| 51 | + } |
| 52 | + |
| 53 | + models.DB = db |
| 54 | + config.Config.Standalone = true |
| 55 | + config.Config.ShamirFeature = false |
| 56 | + config.Config.EmailWhitelist = nil |
| 57 | + config.Config.VerificationCodeExpires = 5 |
| 58 | + auth.InitVerificationCodeCache() |
| 59 | + |
| 60 | + const email = "inactive@example.com" |
| 61 | + oldPassword, err := auth.MakePassword("old-password") |
| 62 | + if err != nil { |
| 63 | + t.Fatal(err) |
| 64 | + } |
| 65 | + user := models.User{ |
| 66 | + Identifier: sql.NullString{String: auth.MakeIdentifier(email), Valid: true}, |
| 67 | + Password: oldPassword, |
| 68 | + } |
| 69 | + if err := db.Create(&user).Error; err != nil { |
| 70 | + t.Fatal(err) |
| 71 | + } |
| 72 | + if err := db.Model(&user).Update("is_active", false).Error; err != nil { |
| 73 | + t.Fatal(err) |
| 74 | + } |
| 75 | + |
| 76 | + code, err := auth.SetVerificationCode(email, "reset") |
| 77 | + if err != nil { |
| 78 | + t.Fatal(err) |
| 79 | + } |
| 80 | + |
| 81 | + app := fiber.New(fiber.Config{ErrorHandler: common.ErrorHandler}) |
| 82 | + app.Put("/register", ChangePassword) |
| 83 | + |
| 84 | + body := fmt.Sprintf( |
| 85 | + `{"email":%q,"password":"new-password","verification":%q}`, |
| 86 | + email, |
| 87 | + code, |
| 88 | + ) |
| 89 | + req := httptest.NewRequest(http.MethodPut, "/register", bytes.NewBufferString(body)) |
| 90 | + req.Header.Set("Content-Type", "application/json") |
| 91 | + resp, err := app.Test(req) |
| 92 | + if err != nil { |
| 93 | + t.Fatal(err) |
| 94 | + } |
| 95 | + if resp.StatusCode != http.StatusForbidden { |
| 96 | + t.Fatalf("expected status %d, got %d", http.StatusForbidden, resp.StatusCode) |
| 97 | + } |
| 98 | + |
| 99 | + var updated models.User |
| 100 | + if err := db.Take(&updated, user.ID).Error; err != nil { |
| 101 | + t.Fatal(err) |
| 102 | + } |
| 103 | + if updated.IsActive { |
| 104 | + t.Fatal("expected inactive user to remain inactive after password reset attempt") |
| 105 | + } |
| 106 | + ok, err := auth.CheckPassword("new-password", updated.Password) |
| 107 | + if err != nil { |
| 108 | + t.Fatal(err) |
| 109 | + } |
| 110 | + if ok { |
| 111 | + t.Fatal("expected inactive user's password not to be changed") |
| 112 | + } |
| 113 | +} |
0 commit comments