- classes.OAuthProvider and classes.OAuthProviderSource; GET /oauth-providers looks the source up per request and keeps the empty body without one - exported RegistrationSettings, RegistrationsFromIP, RefreshTTL, RemoteIP - RegisterOptions.PasswordLess (has_self_set_password=false) and FinishLogin (restore + reactivate mail, last_ip_address, last_seen; no event) - TestOAuthProvidersSourceSmoke, TestSocialHooksSmoke; README
87 lines
2.6 KiB
Go
87 lines
2.6 KiB
Go
package user
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"git.golem15.com/golem15/sm-user-plugin/controllers"
|
|
"git.golem15.com/golem15/sm-user-plugin/models"
|
|
"git.golem15.com/golem15/summercms/modules/postcard"
|
|
)
|
|
|
|
// TestSocialHooksSmoke drives the helpers a social-login plugin calls: a
|
|
// password-less registration and FinishLogin on a soft-deleted account.
|
|
func TestSocialHooksSmoke(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("Postgres-backed; skipped under -short")
|
|
}
|
|
app, gdb := sessionApp(t, "0s")
|
|
ctx := t.Context()
|
|
|
|
fields := map[string]any{
|
|
"email": "social-hooks@example.test",
|
|
"password": "random-password-never-shown",
|
|
"password_confirmation": "random-password-never-shown",
|
|
}
|
|
user, errs, err := controllers.RegisterUser(ctx, app, gdb, fields, controllers.RegisterOptions{
|
|
Activate: true, IP: "203.0.113.7", PasswordLess: true,
|
|
})
|
|
if err != nil || len(errs) > 0 {
|
|
t.Fatalf("RegisterUser: %v %v", err, errs)
|
|
}
|
|
var stored models.User
|
|
if err := gdb.First(&stored, user.ID).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if stored.HasSelfSetPassword {
|
|
t.Fatal("a password-less registration stored has_self_set_password=true")
|
|
}
|
|
if !stored.IsActivated {
|
|
t.Fatal("the registration was not activated")
|
|
}
|
|
if stored.Handle == nil || *stored.Handle == "" {
|
|
t.Fatal("the registration got no handle")
|
|
}
|
|
|
|
// FinishLogin restores a soft-deleted account, records the IP, stamps
|
|
// last_seen and sends exactly one reactivate mail.
|
|
loc := "en"
|
|
soft := &models.User{Email: "social-soft@example.test", Password: "x", PreferredLocale: &loc}
|
|
if err := gdb.Create(soft).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := gdb.Model(soft).Update("deleted_at", time.Now()).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var trashed models.User
|
|
if err := gdb.Unscoped().First(&trashed, soft.ID).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !trashed.DeletedAt.Valid {
|
|
t.Fatal("setup: the user is not soft-deleted")
|
|
}
|
|
mail := &captureMail{}
|
|
if err := app.Publish[postcard.Mailer](mail); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := controllers.FinishLogin(ctx, app, gdb, &trashed, "198.51.100.4"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var after models.User
|
|
if err := gdb.Unscoped().First(&after, soft.ID).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if after.DeletedAt.Valid {
|
|
t.Fatal("FinishLogin did not restore the account")
|
|
}
|
|
if after.LastIPAddress == nil || *after.LastIPAddress != "198.51.100.4" {
|
|
t.Fatalf("last_ip_address = %v", after.LastIPAddress)
|
|
}
|
|
if after.LastSeen == nil {
|
|
t.Fatal("FinishLogin did not stamp last_seen")
|
|
}
|
|
if len(mail.got) != 1 || mail.got[0] != "golem15.user::mail.reactivate-en" {
|
|
t.Fatalf("mails = %#v", mail.got)
|
|
}
|
|
}
|