-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from coddo/release/release-v0.1-beta
Release/release v0.1 beta
- Loading branch information
Showing
94 changed files
with
3,278 additions
and
2,341 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ sftp-config.json | |
*.a | ||
*.so | ||
gost/gost | ||
debug | ||
|
||
# Folders | ||
_obj | ||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package transactionapi | ||
|
||
import ( | ||
"gost/api" | ||
"gost/bll" | ||
"gost/filter" | ||
"gost/filter/apifilter" | ||
"gost/orm/models" | ||
"gost/util" | ||
) | ||
|
||
// TransactionsAPI defines the API endpoint for application transactions of any kind | ||
type TransactionsAPI int | ||
|
||
// GetTransaction endpoint retrieves a certain transaction based on its Id | ||
func (t *TransactionsAPI) GetTransaction(params *api.Request) api.Response { | ||
transactionID, found, err := filter.GetIDParameter("transactionId", params.Form) | ||
|
||
if err != nil { | ||
return api.BadRequest(err) | ||
} | ||
|
||
if !found { | ||
return api.NotFound(err) | ||
} | ||
|
||
return bll.GetTransaction(transactionID) | ||
} | ||
|
||
// CreateTransaction endpoint creates a new transaction with the valid transfer tokens and data | ||
func (t *TransactionsAPI) CreateTransaction(params *api.Request) api.Response { | ||
transaction := &models.Transaction{} | ||
|
||
err := util.DeserializeJSON(params.Body, transaction) | ||
if err != nil || !apifilter.CheckTransactionIntegrity(transaction) { | ||
return api.BadRequest(api.ErrEntityFormat) | ||
} | ||
|
||
return bll.CreateTransaction(transaction) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package transactionapi | ||
|
||
import ( | ||
"gost/api" | ||
"gost/auth/identity" | ||
"gost/orm/models" | ||
"gost/service/transactionservice" | ||
"gost/tests" | ||
"net/http" | ||
"net/url" | ||
"testing" | ||
|
||
"gopkg.in/mgo.v2/bson" | ||
) | ||
|
||
const ( | ||
ActionGet = "GetTransaction" | ||
ActionCreate = "CreateTransaction" | ||
) | ||
|
||
const endpointPath = "/transactions" | ||
|
||
type dummyTransaction struct { | ||
BadField string | ||
} | ||
|
||
func TestTransactionsApi(t *testing.T) { | ||
var id bson.ObjectId | ||
|
||
tests.InitializeServerConfigurations(new(TransactionsAPI)) | ||
|
||
// Cleanup function | ||
defer func() { | ||
recover() | ||
transactionservice.DeleteTransaction(id) | ||
}() | ||
|
||
testPostTransactionInBadFormat(t) | ||
testPostTransactionNotIntegral(t) | ||
id = testPostTransactionInGoodFormat(t) | ||
testGetTransactionWithInexistentIDInDB(t) | ||
testGetTransactionWithBadIDParam(t) | ||
testGetTransactionWithGoodIDParam(t, id) | ||
} | ||
|
||
func testGetTransactionWithInexistentIDInDB(t *testing.T) { | ||
params := url.Values{} | ||
params.Add("transactionId", bson.NewObjectId().Hex()) | ||
|
||
tests.PerformTestRequest(endpointPath, ActionGet, api.GET, http.StatusNotFound, params, nil, t) | ||
} | ||
|
||
func testGetTransactionWithBadIDParam(t *testing.T) { | ||
params := url.Values{} | ||
params.Add("transactionId", "2as456fas4") | ||
|
||
tests.PerformTestRequest(endpointPath, ActionGet, api.GET, http.StatusBadRequest, params, nil, t) | ||
} | ||
|
||
func testGetTransactionWithGoodIDParam(t *testing.T, id bson.ObjectId) { | ||
params := url.Values{} | ||
params.Add("transactionId", id.Hex()) | ||
|
||
rw := tests.PerformTestRequest(endpointPath, ActionGet, api.GET, http.StatusOK, params, nil, t) | ||
|
||
body := rw.Body.String() | ||
if len(body) == 0 { | ||
t.Error("Response body is empty or in a corrupt format:", body) | ||
} | ||
} | ||
|
||
func testPostTransactionInBadFormat(t *testing.T) { | ||
dTransaction := &dummyTransaction{ | ||
BadField: "bad value", | ||
} | ||
|
||
tests.PerformTestRequest(endpointPath, ActionCreate, api.POST, http.StatusBadRequest, nil, dTransaction, t) | ||
} | ||
|
||
func testPostTransactionNotIntegral(t *testing.T) { | ||
transaction := &models.Transaction{ | ||
ID: bson.NewObjectId(), | ||
Payer: identity.ApplicationUser{ID: bson.NewObjectId()}, | ||
Currency: "USD", | ||
} | ||
|
||
tests.PerformTestRequest(endpointPath, ActionCreate, api.POST, http.StatusBadRequest, nil, transaction, t) | ||
} | ||
|
||
func testPostTransactionInGoodFormat(t *testing.T) bson.ObjectId { | ||
transaction := &models.Transaction{ | ||
ID: bson.NewObjectId(), | ||
Payer: identity.ApplicationUser{ID: bson.NewObjectId()}, | ||
Receiver: identity.ApplicationUser{ID: bson.NewObjectId()}, | ||
Type: models.TransactionTypeCash, | ||
Ammount: 216.365, | ||
Currency: "USD", | ||
} | ||
|
||
rw := tests.PerformTestRequest(endpointPath, ActionCreate, api.POST, http.StatusCreated, nil, transaction, t) | ||
|
||
body := rw.Body.String() | ||
if len(body) == 0 { | ||
t.Error("Response body is empty or in deteriorated format:", body) | ||
} | ||
|
||
return transaction.ID | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package api | ||
|
||
import "net/http" | ||
|
||
const ( | ||
// StatusTooManyRequests is used for issuing errors regarding the number of request made by a client | ||
StatusTooManyRequests = 429 | ||
) | ||
|
||
var statusText = map[int]string{ | ||
StatusTooManyRequests: "Too many requests", | ||
} | ||
|
||
// StatusText returns the message associated to a http status code | ||
func StatusText(statusCode int) string { | ||
msg := http.StatusText(statusCode) | ||
if len(msg) > 0 { | ||
return msg | ||
} | ||
|
||
return statusText[statusCode] | ||
} |
Oops, something went wrong.