2fas-server/e2e-tests/helpers.go

123 lines
3.6 KiB
Go
Raw Normal View History

2024-03-16 15:41:30 +01:00
package e2e_tests
2022-12-31 10:22:38 +01:00
import (
"encoding/json"
"fmt"
"testing"
"github.com/twofas/2fas-server/internal/common/crypto"
2022-12-31 10:22:38 +01:00
)
func CreateDevice(t *testing.T, name, fcmToken string) (*DeviceResponse, string) {
keyPair := crypto.GenerateKeyPair(2048)
devicePubKey := crypto.PublicKeyToBase64(keyPair.PublicKey)
payload := []byte(fmt.Sprintf(`{"name":"%s","platform":"android","fcm_token":"%s"}`, name, fcmToken))
device := new(DeviceResponse)
DoAPISuccessPost(t, "mobile/devices", payload, device)
2022-12-31 10:22:38 +01:00
return device, devicePubKey
}
func CreateBrowserExtension(t *testing.T, name string) *BrowserExtensionResponse {
keyPair := crypto.GenerateKeyPair(2048)
pubKey := crypto.PublicKeyToBase64(keyPair.PublicKey)
payload := []byte(fmt.Sprintf(`{"name":"%s","browser_name":"go-browser","browser_version":"0.1","public_key":"%s"}`, name, pubKey))
browserExt := new(BrowserExtensionResponse)
DoAPISuccessPost(t, "/browser_extensions", payload, browserExt)
2022-12-31 10:22:38 +01:00
return browserExt
}
func CreateBrowserExtensionWithPublicKey(t *testing.T, name, publicKey string) *BrowserExtensionResponse {
payload := []byte(fmt.Sprintf(`{"name":"%s","browser_name":"go-browser","browser_version":"0.1","public_key":"%s"}`, name, publicKey))
browserExt := new(BrowserExtensionResponse)
DoAPISuccessPost(t, "/browser_extensions", payload, browserExt)
2022-12-31 10:22:38 +01:00
return browserExt
}
func PairDeviceWithBrowserExtension(t *testing.T, devicePubKey string, browserExtension *BrowserExtensionResponse, device *DeviceResponse) *PairingResultResponse {
payload := struct {
ExtensionId string `json:"extension_id"`
DeviceName string `json:"device_name"`
DevicePublicKey string `json:"device_public_key"`
}{
ExtensionId: browserExtension.Id,
DeviceName: device.Name,
DevicePublicKey: devicePubKey,
}
pairingResult := new(PairingResultResponse)
payloadJson, _ := json.Marshal(payload)
DoAPISuccessPost(t, "/mobile/devices/"+device.Id+"/browser_extensions", payloadJson, pairingResult)
2022-12-31 10:22:38 +01:00
return pairingResult
}
func GetExtensionDevices(t *testing.T, extensionId string) []*ExtensionPairedDeviceResponse {
var extensionDevices []*ExtensionPairedDeviceResponse
DoAPISuccessGet(t, "/browser_extensions/"+extensionId+"/devices", &extensionDevices)
2022-12-31 10:22:38 +01:00
return extensionDevices
}
func Request2FaToken(t *testing.T, domain, extensionId string) *AuthTokenRequestResponse {
var response *AuthTokenRequestResponse
payload := []byte(fmt.Sprintf(`{"domain":"%s"}`, domain))
DoAPISuccessPost(t, "browser_extensions/"+extensionId+"/commands/request_2fa_token", payload, &response)
2022-12-31 10:22:38 +01:00
return response
}
func Send2FaTokenToExtension(t *testing.T, extensionId, deviceId, requestId, token string) {
j := fmt.Sprintf(`{"token_request_id":"%s","extension_id":"%s","token":"%s"}`, requestId, extensionId, token)
DoAPISuccessPost(t, "mobile/devices/"+deviceId+"/commands/send_2fa_token", []byte(j), nil)
}
func RemoveAllBrowserExtensionsDevices(t *testing.T) {
DoAdminSuccessDelete(t, "browser_extensions/devices")
}
func RemoveAllBrowserExtensions(t *testing.T) {
DoAdminSuccessDelete(t, "browser_extensions")
}
func RemoveAllMobileDevices(t *testing.T) {
DoAdminSuccessDelete(t, "/mobile/devices")
}
func RemoveAllMobileIconsCollections(t *testing.T) {
DoAdminSuccessDelete(t, "mobile/icons/collections")
}
func RemoveAllMobileWebServices(t *testing.T) {
DoAdminSuccessDelete(t, "mobile/web_services")
}
func RemoveAllMobileIcons(t *testing.T) {
DoAdminSuccessDelete(t, "mobile/icons")
}
func RemoveAllMobileIconsRequests(t *testing.T) {
DoAdminSuccessDelete(t, "mobile/icons/requests")
}
func RemoveAllMobileNotifications(t *testing.T) {
DoAdminSuccessDelete(t, "mobile/notifications")
2022-12-31 10:22:38 +01:00
}