refactor: format files

master
sundowndev 2019-11-27 17:40:24 +01:00
parent 2cfe1b9185
commit 1be5c7848f
9 changed files with 184 additions and 185 deletions

38
main.go
View File

@ -7,37 +7,37 @@
package main
import (
"log"
"log"
"github.com/joho/godotenv"
"github.com/joho/godotenv"
"github.com/sundowndev/api-go-example/src/routers"
"github.com/sundowndev/api-go-example/src/services"
"github.com/sundowndev/api-go-example/src/routers"
"github.com/sundowndev/api-go-example/src/services"
)
// init
func init() {
// Initialize environment variable contained in .env
if err := godotenv.Load(); err != nil {
log.Fatal("No .env file found")
}
// Initialize environment variable contained in .env
if err := godotenv.Load(); err != nil {
log.Fatal("No .env file found")
}
}
// main
func main() {
// Initialize Database
services.Database()
// Initialize Database
services.Database()
// Initialize Jwt
services.Jwt()
// Initialize Jwt
services.Jwt()
// Initialize router
var routerEngine = routers.RouterInitialize()
// Initialize router
var routerEngine = routers.RouterInitialize()
// Launch API
if err := routerEngine.Run(); err != nil {
log.Fatal(err)
}
// Launch API
if err := routerEngine.Run(); err != nil {
log.Fatal(err)
}
services.Db.Close()
services.Db.Close()
}

View File

@ -8,11 +8,11 @@ package fixture
// DefaultUser
func DefaultUser() map[string]string {
// gofakeit.Seed(time.Now().UnixNano())
// gofakeit.Seed(time.Now().UnixNano())
return map[string]string{
"name": "example",
"email": "example@test.com",
"password": "12345678",
}
return map[string]string{
"name": "example",
"email": "example@test.com",
"password": "12345678",
}
}

View File

@ -7,22 +7,22 @@
package functional
import (
"github.com/stretchr/testify/assert"
"net/url"
"regexp"
"testing"
"github.com/stretchr/testify/assert"
"net/url"
"regexp"
"testing"
)
// TestLogin
func TestLogin(t *testing.T) {
var body map[string]interface{}
var err error
var data = url.Values{"email": {"example@test.com"}, "password": {"12345678"}}
var body map[string]interface{}
var err error
var data = url.Values{"email": {"example@test.com"}, "password": {"12345678"}}
if body, err = RequestApiJson("POST", BaseUrl + "/auth", data, false); err != nil {
t.Fatal(err)
}
if body, err = RequestApiJson("POST", BaseUrl+"/auth", data, false); err != nil {
t.Fatal(err)
}
assert.Regexp(t, regexp.MustCompile(RegexToken), body["access_token"])
assert.Regexp(t, regexp.MustCompile(RegexToken), body["refresh_token"])
assert.Regexp(t, regexp.MustCompile(RegexToken), body["access_token"])
assert.Regexp(t, regexp.MustCompile(RegexToken), body["refresh_token"])
}

View File

@ -7,22 +7,22 @@
package functional
import (
"github.com/stretchr/testify/assert"
"net/url"
"regexp"
"testing"
"github.com/stretchr/testify/assert"
"net/url"
"regexp"
"testing"
)
// TestRefreshToken
func TestRefreshToken(t *testing.T) {
var body map[string]interface{}
var err error
var data = url.Values{"refresh_token": {Tokens.RefreshToken}}
var body map[string]interface{}
var err error
var data = url.Values{"refresh_token": {Tokens.RefreshToken}}
if body, err = RequestApiJson("POST", BaseUrl + "/auth/refresh", data, false); err != nil {
t.Fatal(err)
}
if body, err = RequestApiJson("POST", BaseUrl+"/auth/refresh", data, false); err != nil {
t.Fatal(err)
}
assert.Regexp(t, regexp.MustCompile(RegexToken), body["access_token"])
assert.Regexp(t, regexp.MustCompile(RegexToken), body["refresh_token"])
assert.Regexp(t, regexp.MustCompile(RegexToken), body["access_token"])
assert.Regexp(t, regexp.MustCompile(RegexToken), body["refresh_token"])
}

View File

@ -7,21 +7,21 @@
package functional
import (
"log"
"net/url"
"os"
"testing"
"log"
"net/url"
"os"
"testing"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/sundowndev/api-go-example/src/services"
"github.com/sundowndev/api-go-example/test/fixture"
"github.com/sundowndev/api-go-example/src/services"
"github.com/sundowndev/api-go-example/test/fixture"
)
type Token struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
}
const RegexToken = `^[A-Za-z0-9-_=]+\.[A-Za-z0-9-_=]+\.?[A-Za-z0-9-_.+/=]*$`
@ -29,66 +29,65 @@ const RegexToken = `^[A-Za-z0-9-_=]+\.[A-Za-z0-9-_=]+\.?[A-Za-z0-9-_.+/=]*$`
var BaseUrl string
var Tokens Token
func getToken() {
var body map[string]interface{}
var err error
var body map[string]interface{}
var err error
var user = fixture.DefaultUser()
var data = url.Values{"email": {user["email"]}, "password": {user["password"]}}
var user = fixture.DefaultUser()
var data = url.Values{"email": {user["email"]}, "password": {user["password"]}}
if body, err = RequestApiJson("POST", BaseUrl + "/auth", data, false); err != nil {
log.Fatal(err)
}
if body, err = RequestApiJson("POST", BaseUrl+"/auth", data, false); err != nil {
log.Fatal(err)
}
Tokens.RefreshToken = body["refresh_token"].(string)
Tokens.AccessToken = body["access_token"].(string)
Tokens.RefreshToken = body["refresh_token"].(string)
Tokens.AccessToken = body["access_token"].(string)
}
func setupDB() {
var user = fixture.DefaultUser()
var user = fixture.DefaultUser()
CreateUser(user)
CreateUser(user)
}
func removeDB() {
services.Db.Exec("DROP TABLE users;")
services.Db.Exec("CREATE DATABASE " + os.Getenv("DB_NAME") + ";")
services.Db.Exec("DROP TABLE users;")
services.Db.Exec("CREATE DATABASE " + os.Getenv("DB_NAME") + ";")
}
// init
func init() {
// Initialize environment variable contained in .env
if err := godotenv.Load("../../.env.testing"); err != nil {
log.Fatal("No .env.testing file found")
}
// Initialize environment variable contained in .env
if err := godotenv.Load("../../.env.testing"); err != nil {
log.Fatal("No .env.testing file found")
}
BaseUrl = os.Getenv("HOST") + ":" + os.Getenv("PORT")
BaseUrl = os.Getenv("HOST") + ":" + os.Getenv("PORT")
}
// TestUserPost
func TestMain(m *testing.M) {
var ret int
var ret int
// Set gin mode to test
gin.SetMode(gin.TestMode)
// Set gin mode to test
gin.SetMode(gin.TestMode)
// Initialize Database
services.Database()
// Initialize Database
services.Database()
// Initialize Jwt
services.Jwt()
// Initialize Jwt
services.Jwt()
// Setup DB
setupDB()
// Setup DB
setupDB()
// Get token for test request
getToken()
// Get token for test request
getToken()
ret = m.Run()
ret = m.Run()
// Remove DB
removeDB()
// Remove DB
removeDB()
os.Exit(ret)
os.Exit(ret)
}

View File

@ -7,34 +7,34 @@
package functional
import (
"fmt"
"testing"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/assert"
)
// TestDeleteId
func TestDeleteId(t *testing.T) {
var body map[string]interface{}
var item map[string]interface{}
var err error
var body map[string]interface{}
var item map[string]interface{}
var err error
var user = CreateUser(map[string]string{
"name": "DeletedUser",
"email": "DeletedUser@example.com",
"password": "12345678",
})
var user = CreateUser(map[string]string{
"name": "DeletedUser",
"email": "DeletedUser@example.com",
"password": "12345678",
})
if body, err = RequestApiJson("DELETE", fmt.Sprintf("%s/users/%d", BaseUrl, user.ID), nil, true); err != nil {
t.Fatal(err)
}
if body, err = RequestApiJson("DELETE", fmt.Sprintf("%s/users/%d", BaseUrl, user.ID), nil, true); err != nil {
t.Fatal(err)
}
item = body["item"].(map[string]interface{})
assert.Equal(t, float64(user.ID), item["ID"])
assert.Equal(t, user.Name, item["name"])
assert.Equal(t, user.Email, item["email"])
assert.Nil(t, item["password"])
assert.NotNil(t, item["CreatedAt"])
assert.NotNil(t, item["UpdatedAt"])
// assert.NotNil(t, item["DeletedAt"])
item = body["item"].(map[string]interface{})
assert.Equal(t, float64(user.ID), item["ID"])
assert.Equal(t, user.Name, item["name"])
assert.Equal(t, user.Email, item["email"])
assert.Nil(t, item["password"])
assert.NotNil(t, item["CreatedAt"])
assert.NotNil(t, item["UpdatedAt"])
// assert.NotNil(t, item["DeletedAt"])
}

View File

@ -7,29 +7,29 @@
package functional
import (
"testing"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/assert"
"github.com/sundowndev/api-go-example/test/fixture"
"github.com/sundowndev/api-go-example/test/fixture"
)
// TestGetId
func TestGetId(t *testing.T) {
var body map[string]interface{}
var item map[string]interface{}
var err error
var user = fixture.DefaultUser()
var body map[string]interface{}
var item map[string]interface{}
var err error
var user = fixture.DefaultUser()
if body, err = RequestApiJson("GET", BaseUrl + "/users/1", nil, true); err != nil {
t.Fatal(err)
}
if body, err = RequestApiJson("GET", BaseUrl+"/users/1", nil, true); err != nil {
t.Fatal(err)
}
item = body["item"].(map[string]interface{})
assert.Equal(t, float64(1), item["ID"])
assert.Equal(t, user["name"], item["name"])
assert.Equal(t, user["email"], item["email"])
assert.Nil(t, item["password"])
assert.NotNil(t, item["CreatedAt"])
assert.NotNil(t, item["UpdatedAt"])
item = body["item"].(map[string]interface{})
assert.Equal(t, float64(1), item["ID"])
assert.Equal(t, user["name"], item["name"])
assert.Equal(t, user["email"], item["email"])
assert.Nil(t, item["password"])
assert.NotNil(t, item["CreatedAt"])
assert.NotNil(t, item["UpdatedAt"])
}

View File

@ -7,28 +7,28 @@
package functional
import (
"net/url"
"testing"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/assert"
)
// TestUserPost
func TestUserPost(t *testing.T) {
var body map[string]interface{}
var item map[string]interface{}
var err error
var data = url.Values{"name": {"test1"}, "email": {"example@test1.com"}, "password": {"12345678"}}
var body map[string]interface{}
var item map[string]interface{}
var err error
var data = url.Values{"name": {"test1"}, "email": {"example@test1.com"}, "password": {"12345678"}}
if body, err = RequestApiJson("POST", BaseUrl + "/users", data, true); err != nil {
t.Fatal(err)
}
if body, err = RequestApiJson("POST", BaseUrl+"/users", data, true); err != nil {
t.Fatal(err)
}
item = body["item"].(map[string]interface{})
assert.Equal(t, data.Get("name"), item["name"])
assert.Equal(t, data.Get("email"), item["email"])
assert.Nil(t, item["password"])
assert.NotNil(t, item["ID"])
assert.NotNil(t, item["CreatedAt"])
assert.NotNil(t, item["UpdatedAt"])
item = body["item"].(map[string]interface{})
assert.Equal(t, data.Get("name"), item["name"])
assert.Equal(t, data.Get("email"), item["email"])
assert.Nil(t, item["password"])
assert.NotNil(t, item["ID"])
assert.NotNil(t, item["CreatedAt"])
assert.NotNil(t, item["UpdatedAt"])
}

View File

@ -7,58 +7,58 @@
package functional
import (
"encoding/json"
"log"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"encoding/json"
"log"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"github.com/sundowndev/api-go-example/src/models"
"github.com/sundowndev/api-go-example/src/routers"
"github.com/sundowndev/api-go-example/src/services"
"github.com/sundowndev/api-go-example/src/models"
"github.com/sundowndev/api-go-example/src/routers"
"github.com/sundowndev/api-go-example/src/services"
)
// RequestApiJson
func RequestApiJson(method, url string, data url.Values, auth bool) (map[string]interface{}, error) {
var result map[string]interface{}
var req *http.Request
var res *httptest.ResponseRecorder
var err error
var result map[string]interface{}
var req *http.Request
var res *httptest.ResponseRecorder
var err error
routerEngine := routers.RouterInitialize()
routerEngine := routers.RouterInitialize()
if req, err = http.NewRequest(method, url, strings.NewReader(data.Encode())); err != nil {
return result, err
}
if req, err = http.NewRequest(method, url, strings.NewReader(data.Encode())); err != nil {
return result, err
}
if auth == true {
req.Header.Add("Authorization", `Bearer ` + Tokens.AccessToken)
}
if auth == true {
req.Header.Add("Authorization", `Bearer `+Tokens.AccessToken)
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
res = httptest.NewRecorder()
routerEngine.ServeHTTP(res, req)
res = httptest.NewRecorder()
routerEngine.ServeHTTP(res, req)
if err = json.Unmarshal(res.Body.Bytes(), &result); err != nil {
return result, err
}
if err = json.Unmarshal(res.Body.Bytes(), &result); err != nil {
return result, err
}
return result, err
return result, err
}
// CreateUser
func CreateUser(data map[string]string) models.User {
var user = models.User{
Name: data["name"],
Email: data["email"],
Password: data["password"],
}
var user = models.User{
Name: data["name"],
Email: data["email"],
Password: data["password"],
}
if err := services.Db.Create(&user).Error; err != nil {
log.Fatal(err)
}
if err := services.Db.Create(&user).Error; err != nil {
log.Fatal(err)
}
return user
return user
}