forked from ebhomengo/niki
54 lines
1.4 KiB
Go
54 lines
1.4 KiB
Go
//go:build end2end
|
|
|
|
package end2end
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactor"
|
|
"github.com/labstack/echo/v4"
|
|
"log"
|
|
"net/http/httptest"
|
|
)
|
|
|
|
// CreateRequest is a utility function to create and send HTTP requests
|
|
func CreateRequest(method, url, token string, body interface{}) *httptest.ResponseRecorder {
|
|
var buf bytes.Buffer
|
|
if body != nil {
|
|
err := json.NewEncoder(&buf).Encode(body)
|
|
if err != nil {
|
|
log.Fatalf("could not encode body: %v", err)
|
|
}
|
|
}
|
|
|
|
req := httptest.NewRequest(method, url, &buf)
|
|
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
|
|
req.Header.Set(echo.HeaderAuthorization, fmt.Sprintf("Bearer %s", token))
|
|
|
|
rec := httptest.NewRecorder()
|
|
testServer.Serve(rec, req)
|
|
return rec
|
|
}
|
|
|
|
// LoginBenefactor is a utility function to login a benefactor and return the access token
|
|
func LoginBenefactor(phoneNumber string) string {
|
|
sendOTPRes, err := services.BenefactorSvc.SendOtp(context.Background(), param.SendOtpRequest{
|
|
PhoneNumber: phoneNumber,
|
|
})
|
|
if err != nil {
|
|
log.Fatalf("failed to send OTP: %v", err)
|
|
}
|
|
|
|
registerRes, err := services.BenefactorSvc.LoginOrRegister(context.Background(), param.LoginOrRegisterRequest{
|
|
PhoneNumber: phoneNumber,
|
|
VerificationCode: sendOTPRes.Code,
|
|
})
|
|
if err != nil {
|
|
log.Fatalf("failed to register: %v", err)
|
|
}
|
|
|
|
return registerRes.Tokens.AccessToken
|
|
}
|