-
Notifications
You must be signed in to change notification settings - Fork 0
/
SignUpTest.go
104 lines (84 loc) · 2.49 KB
/
SignUpTest.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
package test
import (
"bytes"
"encoding/json"
"fmt"
"math/rand"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"time"
"github.com/ribeirosaimon/motion-go/baseapp/pkg/router"
"github.com/ribeirosaimon/motion-go/internal/db"
"github.com/ribeirosaimon/motion-go/internal/domain/sqlDomain"
"github.com/ribeirosaimon/motion-go/internal/repository"
)
var (
testJwtToken string
user SignUpTestDto
)
func Token(roles ...sqlDomain.RoleEnum) (string, SignUpTestDto) {
if testJwtToken != "" {
return testJwtToken, user
}
return signUp(roles...)
}
func signUp(roles ...sqlDomain.RoleEnum) (string, SignUpTestDto) {
user := createUser(roles...)
jsonData, _ := json.Marshal(user)
e := CreateEngine(router.NewLoginRouter)
req, _ := http.NewRequest(http.MethodPost, "/auth/sign-up", bytes.NewReader(jsonData))
resp := httptest.NewRecorder()
e.ServeHTTP(resp, req)
var signProfileResponse = sqlDomain.Profile{}
err := json.Unmarshal(resp.Body.Bytes(), &signProfileResponse)
if err != nil {
panic(err)
}
dto := loginTestDto{Email: user.Email, Password: user.Password}
jsonLoginDto, err := json.Marshal(dto)
loginResp := httptest.NewRecorder()
loginReq, _ := http.NewRequest(http.MethodPost, "/auth/login", bytes.NewReader(jsonLoginDto))
e.ServeHTTP(loginResp, loginReq)
return strings.Replace(string(loginResp.Body.Bytes()), "\"", "", -1), user
}
func createUser(roles ...sqlDomain.RoleEnum) SignUpTestDto {
rand.Seed(time.Now().UnixNano())
nameRandom := strconv.Itoa(rand.Intn(1000000))
password := strconv.Itoa(rand.Intn(1000000))
emailRandom := fmt.Sprintf("%s@email.com", strconv.Itoa(rand.Intn(1000000)))
var dto SignUpTestDto
dto.Name = nameRandom
dto.Password = password
dto.Email = emailRandom
allRoles := []sqlDomain.Role{
{
Name: sqlDomain.ADMIN,
}, {
Name: sqlDomain.USER,
},
}
roleRepository := repository.NewRoleRepository(db.Conn.GetPgsqTemplate())
for _, v := range allRoles {
if !roleRepository.ExistByField("name", v.Name) {
roleRepository.Save(v)
}
}
for _, v := range roles {
r, _ := roleRepository.FindByField("name", v)
dto.Roles = append(dto.Roles, r.Name)
}
return dto
}
type SignUpTestDto struct {
loginTestDto
Name string `json:"name"`
Roles []sqlDomain.RoleEnum `json:"roles"`
}
type loginTestDto struct {
Email string `json:"email"`
Password string `json:"password,omitempty"`
Token string `json:"token"`
LoggedRole sqlDomain.RoleEnum `json:"loggedRole"`
}