This repository has been archived by the owner on Sep 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
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 #71 from poliha/tx_status
Tx status
- Loading branch information
Showing
13 changed files
with
782 additions
and
5 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
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
85 changes: 85 additions & 0 deletions
85
src/github.com/stellar/gateway/compliance/handlers/request_handler_tx_status.go
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,85 @@ | ||
package handlers | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"net/http" | ||
"net/url" | ||
|
||
log "github.com/Sirupsen/logrus" | ||
"github.com/stellar/gateway/protocols" | ||
"github.com/stellar/gateway/server" | ||
"github.com/stellar/go/protocols/compliance" | ||
) | ||
|
||
// HandlerTxStatus implements /tx_status endpoint | ||
func (rh *RequestHandler) HandlerTxStatus(w http.ResponseWriter, r *http.Request) { | ||
|
||
txid := r.URL.Query().Get("id") | ||
if txid == "" { | ||
log.Info("unable to get query parameter") | ||
server.Write(w, protocols.NewMissingParameter("id")) | ||
return | ||
} | ||
response := compliance.TransactionStatusResponse{} | ||
|
||
if rh.Config.Callbacks.TxStatus == "" { | ||
response.Status = compliance.TransactionStatusUnknown | ||
} else { | ||
|
||
u, err := url.Parse(rh.Config.Callbacks.TxStatus) | ||
if err != nil { | ||
log.Error(err, "failed to parse tx status endpoint") | ||
server.Write(w, protocols.InternalServerError) | ||
return | ||
} | ||
|
||
q := u.Query() | ||
q.Set("id", txid) | ||
u.RawQuery = q.Encode() | ||
resp, err := rh.Client.Get(u.String()) | ||
if err != nil { | ||
log.WithFields(log.Fields{ | ||
"tx_status": u.String(), | ||
"err": err, | ||
}).Error("Error sending request to tx_status server") | ||
server.Write(w, protocols.InternalServerError) | ||
return | ||
} | ||
|
||
defer resp.Body.Close() | ||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
log.Error("Error reading tx_status server response") | ||
server.Write(w, protocols.InternalServerError) | ||
return | ||
} | ||
|
||
switch resp.StatusCode { | ||
case http.StatusOK: | ||
err := json.Unmarshal(body, &response) | ||
if err != nil { | ||
log.WithFields(log.Fields{ | ||
"tx_status": rh.Config.Callbacks.TxStatus, | ||
"body": string(body), | ||
}).Error("Unable to decode tx_status response") | ||
server.Write(w, protocols.InternalServerError) | ||
return | ||
} | ||
if response.Status == "" { | ||
response.Status = compliance.TransactionStatusUnknown | ||
} | ||
|
||
default: | ||
response.Status = compliance.TransactionStatusUnknown | ||
} | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
err := json.NewEncoder(w).Encode(response) | ||
if err != nil { | ||
log.Error("Error encoding tx status response") | ||
server.Write(w, protocols.InternalServerError) | ||
return | ||
} | ||
} |
157 changes: 157 additions & 0 deletions
157
src/github.com/stellar/gateway/compliance/handlers/request_handler_tx_status_test.go
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,157 @@ | ||
package handlers | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/facebookgo/inject" | ||
"github.com/goji/httpauth" | ||
. "github.com/smartystreets/goconvey/convey" | ||
"github.com/stellar/gateway/compliance/config" | ||
"github.com/stellar/gateway/mocks" | ||
"github.com/stellar/gateway/net" | ||
"github.com/stellar/go/support/http/httptest" | ||
) | ||
|
||
func TestRequestHandlerTxStatus(t *testing.T) { | ||
c := &config.Config{ | ||
NetworkPassphrase: "Test SDF Network ; September 2015", | ||
Keys: config.Keys{ | ||
// GBYJZW5XFAI6XV73H5SAIUYK6XZI4CGGVBUBO3ANA2SV7KKDAXTV6AEB | ||
SigningSeed: "SDWTLFPALQSP225BSMX7HPZ7ZEAYSUYNDLJ5QI3YGVBNRUIIELWH3XUV", | ||
}, | ||
} | ||
c.TxStatusAuth.Username = "username" | ||
c.TxStatusAuth.Password = "password" | ||
|
||
txid := "abc123" | ||
mockHTTPClient := new(mocks.MockHTTPClient) | ||
mockEntityManager := new(mocks.MockEntityManager) | ||
mockRepository := new(mocks.MockRepository) | ||
mockFederationResolver := new(mocks.MockFederationResolver) | ||
mockSignerVerifier := new(mocks.MockSignerVerifier) | ||
mockStellartomlResolver := new(mocks.MockStellartomlResolver) | ||
requestHandler := RequestHandler{} | ||
|
||
// Inject mocks | ||
var g inject.Graph | ||
|
||
err := g.Provide( | ||
&inject.Object{Value: &requestHandler}, | ||
&inject.Object{Value: c}, | ||
&inject.Object{Value: mockHTTPClient}, | ||
&inject.Object{Value: mockEntityManager}, | ||
&inject.Object{Value: mockRepository}, | ||
&inject.Object{Value: mockFederationResolver}, | ||
&inject.Object{Value: mockSignerVerifier}, | ||
&inject.Object{Value: mockStellartomlResolver}, | ||
&inject.Object{Value: &TestNonceGenerator{}}, | ||
) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
if err := g.Populate(); err != nil { | ||
panic(err) | ||
} | ||
|
||
httpHandle := func(w http.ResponseWriter, r *http.Request) { | ||
requestHandler.HandlerTxStatus(w, r) | ||
} | ||
|
||
testServer := httptest.NewServer(t, httpauth.SimpleBasicAuth(c.TxStatusAuth.Username, | ||
c.TxStatusAuth.Password)(http.HandlerFunc(httpHandle))) | ||
defer testServer.Close() | ||
|
||
Convey("Given tx_status request", t, func() { | ||
Convey("it returns unauthorised when no auth", func() { | ||
testServer.GET("/tx_status"). | ||
WithQuery("id", "123"). | ||
Expect(). | ||
Status(http.StatusUnauthorized) | ||
}) | ||
Convey("it returns unauthorised when bad auth", func() { | ||
testServer.GET("/tx_status"). | ||
WithBasicAuth("username", "wrong_password"). | ||
Expect(). | ||
Status(http.StatusUnauthorized) | ||
}) | ||
Convey("it returns bad request when no parameter", func() { | ||
testServer.GET("/tx_status"). | ||
WithBasicAuth("username", "password"). | ||
Expect(). | ||
Status(http.StatusBadRequest) | ||
}) | ||
Convey("it returns unknown when no tx_status endpoint", func() { | ||
testServer.GET("/tx_status"). | ||
WithBasicAuth("username", "password"). | ||
WithQuery("id", "123"). | ||
Expect(). | ||
Status(http.StatusOK). | ||
Body().Equal(`{"status":"unknown"}` + "\n") | ||
}) | ||
Convey("it returns unknown when valid endpoint returns bad request", func() { | ||
c.Callbacks = config.Callbacks{ | ||
TxStatus: "http://tx_status", | ||
} | ||
|
||
mockHTTPClient.On( | ||
"Get", | ||
"http://tx_status?id="+txid, | ||
).Return( | ||
net.BuildHTTPResponse(400, "badrequest"), | ||
nil, | ||
).Once() | ||
|
||
testServer.GET("/tx_status"). | ||
WithBasicAuth("username", "password"). | ||
WithQuery("id", txid). | ||
Expect(). | ||
Status(http.StatusOK). | ||
Body().Equal(`{"status":"unknown"}` + "\n") | ||
}) | ||
|
||
Convey("it returns unknown when valid endpoint returns empty data", func() { | ||
c.Callbacks = config.Callbacks{ | ||
TxStatus: "http://tx_status", | ||
} | ||
|
||
mockHTTPClient.On( | ||
"Get", | ||
"http://tx_status?id="+txid, | ||
).Return( | ||
net.BuildHTTPResponse(200, "{}"), | ||
nil, | ||
).Once() | ||
|
||
testServer.GET("/tx_status"). | ||
WithBasicAuth("username", "password"). | ||
WithQuery("id", txid). | ||
Expect(). | ||
Status(http.StatusOK). | ||
Body().Equal(`{"status":"unknown"}` + "\n") | ||
}) | ||
|
||
Convey("it returns response from valid endpoint with data", func() { | ||
c.Callbacks = config.Callbacks{ | ||
TxStatus: "http://tx_status", | ||
} | ||
|
||
mockHTTPClient.On( | ||
"Get", | ||
"http://tx_status?id="+txid, | ||
).Return( | ||
net.BuildHTTPResponse(200, `{"status":"delivered","msg":"cash paid"}`), | ||
nil, | ||
).Once() | ||
|
||
testServer.GET("/tx_status"). | ||
WithBasicAuth("username", "password"). | ||
WithQuery("id", txid). | ||
Expect(). | ||
Status(http.StatusOK). | ||
Body().Equal(`{"status":"delivered","msg":"cash paid"}` + "\n") | ||
}) | ||
|
||
}) | ||
} |
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
Oops, something went wrong.