83 lines
2.5 KiB
Go
83 lines
2.5 KiB
Go
// Package logictest wires login + in-memory device registry for HTTP/integration tests.
|
|
package logictest
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
authlogic "spark-server/internal/module/auth/logic"
|
|
deviceentity "spark-server/internal/module/device/entity"
|
|
devicelogic "spark-server/internal/module/device/logic"
|
|
devicemodel "spark-server/internal/module/device/model"
|
|
)
|
|
|
|
const (
|
|
TestAppID = uint8(1)
|
|
TestIOSIDFV = "00000000-0000-0000-0000-000000000007"
|
|
TestAndroidID = "spark-test-android-9"
|
|
)
|
|
|
|
var (
|
|
TestIOSDeviceID = mustDeviceID(deviceentity.TypeIOS, TestIOSIDFV, "")
|
|
TestAndroidDeviceID = mustDeviceID(deviceentity.TypeAndroid, "", TestAndroidID)
|
|
)
|
|
|
|
func mustDeviceID(dtype deviceentity.DeviceType, idfv, androidID string) uint64 {
|
|
id, _, err := devicelogic.DeviceIDFromFingerprint(devicelogic.FingerprintInput{
|
|
DeviceType: dtype,
|
|
IDFV: idfv,
|
|
AndroidID: androidID,
|
|
})
|
|
if err != nil || id == 0 {
|
|
panic(err)
|
|
}
|
|
return id
|
|
}
|
|
|
|
// LoginService returns LoginService with memory device registry.
|
|
func LoginService(j authlogic.Issuer, users authlogic.Verifier) *authlogic.LoginService {
|
|
reg := devicemodel.NewMemoryRegistry()
|
|
devices := devicelogic.NewService(reg)
|
|
svc := authlogic.NewLoginService(j, users)
|
|
svc.Devices = devices
|
|
return svc
|
|
}
|
|
|
|
// LoginBodyJSON is a minimal iOS login body for tests (stable device_id via TestIOSIDFV).
|
|
func LoginBodyJSON(email, verifyCode string) string {
|
|
return LoginBodyJSONNamed(email, verifyCode, "")
|
|
}
|
|
|
|
// LoginBodyJSONNamed adds optional device_name.
|
|
func LoginBodyJSONNamed(email, verifyCode, deviceName string) string {
|
|
m := map[string]any{
|
|
"email": email,
|
|
"verify_code": verifyCode,
|
|
"app_id": TestAppID,
|
|
"device_type": int(deviceentity.TypeIOS),
|
|
"idfv": TestIOSIDFV,
|
|
}
|
|
if deviceName != "" {
|
|
m["device_name"] = deviceName
|
|
}
|
|
b, _ := json.Marshal(m)
|
|
return string(b)
|
|
}
|
|
|
|
// LoginBodyJSONWithDevice builds login JSON with explicit identity fields.
|
|
func LoginBodyJSONWithDevice(email, verifyCode string, deviceType uint8, idfv, androidID string) string {
|
|
b, _ := json.Marshal(map[string]any{
|
|
"email": email,
|
|
"verify_code": verifyCode,
|
|
"app_id": TestAppID,
|
|
"device_type": deviceType,
|
|
"idfv": idfv,
|
|
"android_id": androidID,
|
|
})
|
|
return string(b)
|
|
}
|
|
|
|
// LoginBodyJSONAndroid is login with android_id (stable TestAndroidDeviceID).
|
|
func LoginBodyJSONAndroid(email, verifyCode string) string {
|
|
return LoginBodyJSONWithDevice(email, verifyCode, uint8(deviceentity.TypeAndroid), "", TestAndroidID)
|
|
}
|