-
Notifications
You must be signed in to change notification settings - Fork 0
/
testCommon.go
148 lines (126 loc) · 4.04 KB
/
testCommon.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package tests
import (
"encoding/json"
"io/ioutil"
"log"
"math/rand"
"net/http"
"net/http/httptest"
"net/url"
"os"
"testing"
"time"
"github.com/SecurityGeekIO/zscaler-sdk-go/v2/logger"
"github.com/SecurityGeekIO/zscaler-sdk-go/v2/zcon"
"github.com/SecurityGeekIO/zscaler-sdk-go/v2/zia"
"github.com/SecurityGeekIO/zscaler-sdk-go/v2/zpa"
)
const (
charSetAlphaUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
charSetAlphaLower = "abcdefghijklmnopqrstuvwxyz"
charSetNumeric = "0123456789"
charSetSpecialChar = "!@#$%^&*"
)
func init() {
rand.Seed(time.Now().UTC().UnixNano())
}
func TestPassword(length int) string {
if length < 8 {
length = 8
} else if length > 100 {
length = 100
}
result := make([]byte, length)
result[0] = charSetAlphaLower[rand.Intn(len(charSetAlphaLower))]
result[1] = charSetAlphaUpper[rand.Intn(len(charSetAlphaUpper))]
result[2] = charSetNumeric[rand.Intn(len(charSetNumeric))]
result[3] = charSetSpecialChar[rand.Intn(len(charSetSpecialChar))]
charSetAll := charSetAlphaLower + charSetAlphaUpper + charSetNumeric + charSetSpecialChar
for i := 4; i < length; i++ {
result[i] = charSetAll[rand.Intn(len(charSetAll))]
}
// Shuffle the result to avoid predictable patterns (lower, upper, numeric, special)
rand.Shuffle(len(result), func(i, j int) {
result[i], result[j] = result[j], result[i]
})
return string(result)
}
func NewZpaClient() (*zpa.Client, error) {
zpa_client_id := os.Getenv("ZPA_CLIENT_ID")
zpa_client_secret := os.Getenv("ZPA_CLIENT_SECRET")
zpa_customer_id := os.Getenv("ZPA_CUSTOMER_ID")
zpa_cloud := os.Getenv("ZPA_CLOUD")
config, err := zpa.NewConfig(zpa_client_id, zpa_client_secret, zpa_customer_id, zpa_cloud, "zscaler-sdk-go")
if err != nil {
log.Printf("[ERROR] creating config failed: %v\n", err)
return nil, err
}
zpaClient := zpa.NewClient(config)
return zpaClient, nil
}
func NewZpaClientMock() (*zpa.Client, *http.ServeMux, *httptest.Server) {
mux := http.NewServeMux()
// Create a request handler for the exact endpoint
mux.HandleFunc("/signin", func(w http.ResponseWriter, r *http.Request) {
// Write a JSON response
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"token_type": "bearer", "access_token": "Group 1"}`))
})
// Create a test server using the ServeMux
server := httptest.NewServer(mux)
serverURL, _ := url.Parse(server.URL)
// Create a client and set the base URL to the mock server URL
client := &zpa.Client{
Config: &zpa.Config{
ClientID: "clientid",
ClientSecret: "clientsecret",
CustomerID: "customerid",
Logger: logger.NewNopLogger(),
BaseURL: serverURL,
},
}
return client, mux, server
}
func NewZiaClient() (*zia.Client, error) {
username := os.Getenv("ZIA_USERNAME")
password := os.Getenv("ZIA_PASSWORD")
apiKey := os.Getenv("ZIA_API_KEY")
ziaCloud := os.Getenv("ZIA_CLOUD")
cli, err := zia.NewClient(username, password, apiKey, ziaCloud, "zscaler-sdk-go")
if err != nil {
log.Printf("[ERROR] creating client failed: %v\n", err)
return nil, err
}
return cli, nil
}
func NewZConClient() (*zcon.Client, error) {
username := os.Getenv("ZCON_USERNAME")
password := os.Getenv("ZCON_PASSWORD")
apiKey := os.Getenv("ZCON_API_KEY")
zconCloud := os.Getenv("ZCON_CLOUD")
cli, err := zcon.NewClient(username, password, apiKey, zconCloud, "zscaler-sdk-go")
if err != nil {
log.Printf("[ERROR] creating client failed: %v\n", err)
return nil, err
}
return cli, nil
}
// ParseJSONRequest parses the JSON request body from the given HTTP request.
func ParseJSONRequest(t *testing.T, r *http.Request, v interface{}) error {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
defer r.Body.Close()
return json.Unmarshal(body, v)
}
// WriteJSONResponse writes the JSON response with the given status code and data to the HTTP response writer.
func WriteJSONResponse(t *testing.T, w http.ResponseWriter, statusCode int, data interface{}) error {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
if data != nil {
encoder := json.NewEncoder(w)
return encoder.Encode(data)
}
return nil
}