80 lines
2.0 KiB
Go
80 lines
2.0 KiB
Go
package http
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.javil.eu/jacob1123/budgeteer/bcrypt"
|
|
"git.javil.eu/jacob1123/budgeteer/jwt"
|
|
"git.javil.eu/jacob1123/budgeteer/postgres"
|
|
"github.com/gin-gonic/gin"
|
|
|
|
txdb "github.com/DATA-DOG/go-txdb"
|
|
)
|
|
|
|
func init() {
|
|
txdb.Register("pgtx", "pgx", "postgres://budgeteer_test:budgeteer_test@localhost:5432/budgeteer_test")
|
|
}
|
|
|
|
func TestListTimezonesHandler(t *testing.T) {
|
|
db, err := postgres.Connect("pgtx", "example")
|
|
if err != nil {
|
|
t.Errorf("could not connect to db: %s", err)
|
|
return
|
|
}
|
|
|
|
h := Handler{
|
|
Service: db,
|
|
TokenVerifier: &jwt.TokenVerifier{},
|
|
CredentialsVerifier: &bcrypt.Verifier{},
|
|
}
|
|
|
|
recorder := httptest.NewRecorder()
|
|
c, engine := gin.CreateTestContext(recorder)
|
|
h.LoadRoutes(engine)
|
|
|
|
t.Run("RegisterUser", func(t *testing.T) {
|
|
c.Request, err = http.NewRequest(http.MethodPost, "/api/v1/user/register", strings.NewReader(`{"password":"pass","email":"info@example.com","name":"Test"}`))
|
|
if err != nil {
|
|
t.Errorf("error creating request: %s", err)
|
|
return
|
|
}
|
|
|
|
h.registerPost(c)
|
|
|
|
if recorder.Code != http.StatusOK {
|
|
t.Errorf("handler returned wrong status code: got %v want %v", recorder.Code, http.StatusOK)
|
|
}
|
|
|
|
var response LoginResponse
|
|
err = json.NewDecoder(recorder.Body).Decode(&response)
|
|
if err != nil {
|
|
t.Error(err.Error())
|
|
t.Error("Error registering")
|
|
}
|
|
if len(response.Token) == 0 {
|
|
t.Error("Did not get a token")
|
|
}
|
|
})
|
|
|
|
t.Run("GetTransactions", func(t *testing.T) {
|
|
c.Request, err = http.NewRequest(http.MethodGet, "/account/accountid/transactions", nil)
|
|
if recorder.Code != http.StatusOK {
|
|
t.Errorf("handler returned wrong status code: got %v want %v", recorder.Code, http.StatusOK)
|
|
}
|
|
|
|
var response TransactionsResponse
|
|
err = json.NewDecoder(recorder.Body).Decode(&response)
|
|
if err != nil {
|
|
t.Error(err.Error())
|
|
t.Error("Error retreiving list of transactions.")
|
|
}
|
|
if len(response.Transactions) == 0 {
|
|
t.Error("Did not get any transactions.")
|
|
}
|
|
})
|
|
}
|