-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
helpers.go
122 lines (83 loc) · 3.62 KB
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package e2e_tests
import (
"encoding/json"
"fmt"
"testing"
"github.com/twofas/2fas-server/internal/common/crypto"
)
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)
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)
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)
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)
return pairingResult
}
func GetExtensionDevices(t *testing.T, extensionId string) []*ExtensionPairedDeviceResponse {
var extensionDevices []*ExtensionPairedDeviceResponse
DoAPISuccessGet(t, "/browser_extensions/"+extensionId+"/devices", &extensionDevices)
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)
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")
}