mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-06 22:43:10 -06:00
Merge branch 'AllMightLegend/issue3333' of https://github.com/AllMightLegend/dendrite into AllMightLegend/issue3333
This commit is contained in:
commit
a3ced0d5a3
|
|
@ -1,80 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"database/sql"
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"testing"
|
|
||||||
_ "github.com/lib/pq" // PostgreSQL driver
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
connStr = "user=youruser dbname=yourdb sslmode=disable" // replace with your actual connection string
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestPasswordValidationWithPostgres(t *testing.T) {
|
|
||||||
// Connect to the database
|
|
||||||
db, err := sql.Open("postgres", connStr)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to connect to database: %v", err)
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
// Create a table for testing
|
|
||||||
_, err = db.ExecContext(ctx, `
|
|
||||||
CREATE TEMPORARY TABLE users (
|
|
||||||
id SERIAL PRIMARY KEY,
|
|
||||||
username VARCHAR(255),
|
|
||||||
password VARCHAR(255)
|
|
||||||
)
|
|
||||||
`)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to create temporary table: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Define test cases
|
|
||||||
tests := []struct {
|
|
||||||
password string
|
|
||||||
expectErr bool
|
|
||||||
}{
|
|
||||||
{"short", true}, // Password too short
|
|
||||||
{"ValidP@ssw0rd", false}, // Valid password
|
|
||||||
{"WithoutUpperCase1!", true}, // Missing uppercase letter
|
|
||||||
{"withoutlowercase1!", true}, // Missing lowercase letter
|
|
||||||
{"WithoutDigit!", true}, // Missing digit
|
|
||||||
{"WithoutSpecial1", true}, // Missing special character
|
|
||||||
{string(make([]byte, maxPasswordLength+1)), true}, // Password too long
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(fmt.Sprintf("password: %s", tt.password), func(t *testing.T) {
|
|
||||||
err := ValidatePassword(tt.password)
|
|
||||||
if (err != nil) != tt.expectErr {
|
|
||||||
t.Errorf("ValidatePassword(%s) = %v, expected error = %v", tt.password, err, tt.expectErr)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !tt.expectErr {
|
|
||||||
// Insert the valid password into the database
|
|
||||||
_, err = db.ExecContext(ctx, "INSERT INTO users (username, password) VALUES ($1, $2)", "testuser", tt.password)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to insert password into database: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Retrieve the password back from the database
|
|
||||||
var storedPassword string
|
|
||||||
err = db.QueryRowContext(ctx, "SELECT password FROM users WHERE username=$1", "testuser").Scan(&storedPassword)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to retrieve password from database: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if the stored password matches the original
|
|
||||||
if storedPassword != tt.password {
|
|
||||||
t.Errorf("Stored password does not match original. Got %s, want %s", storedPassword, tt.password)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,33 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import "testing"
|
|
||||||
|
|
||||||
func TestValidatePassword(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
password string
|
|
||||||
config PasswordConfig
|
|
||||||
expectErr bool
|
|
||||||
}{
|
|
||||||
// Test cases for length
|
|
||||||
{"short", defaultPasswordConfig, true},
|
|
||||||
{"longEnoughPassword1!", defaultPasswordConfig, false},
|
|
||||||
{string(make([]byte, maxPasswordLength+1)), defaultPasswordConfig, true},
|
|
||||||
|
|
||||||
// Test cases for character requirements
|
|
||||||
{"NoDigitsOrSpecialChars", defaultPasswordConfig, true},
|
|
||||||
{"WithDigits1", defaultPasswordConfig, true},
|
|
||||||
{"WithSpecialChars!", defaultPasswordConfig, true},
|
|
||||||
{"ValidP@ssw0rd", defaultPasswordConfig, false},
|
|
||||||
|
|
||||||
// Custom config examples
|
|
||||||
{"NoSpecialChar123", PasswordConfig{minPasswordLength, maxPasswordLength, true, true, true, false}, false},
|
|
||||||
{"alllowercase1!", PasswordConfig{minPasswordLength, maxPasswordLength, false, true, true, true}, false},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range tests {
|
|
||||||
err := ValidatePassword(tt.password, tt.config)
|
|
||||||
if (err != nil) != tt.expectErr {
|
|
||||||
t.Errorf("ValidatePassword(%s) = %v, expected error = %v", tt.password, err, tt.expectErr)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in a new issue