|
| 1 | +package handlers_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/hex" |
| 6 | + "encoding/json" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "net/http/httptest" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/gorilla/mux" |
| 13 | + "github.com/sprintertech/sprinter-signing/api/handlers" |
| 14 | + across "github.com/sprintertech/sprinter-signing/chains/evm/message" |
| 15 | + "github.com/sprintertech/sprinter-signing/tss/ecdsa/signing" |
| 16 | + "github.com/stretchr/testify/suite" |
| 17 | + "github.com/sygmaprotocol/sygma-core/relayer/message" |
| 18 | +) |
| 19 | + |
| 20 | +type UnlockHandlerTestSuite struct { |
| 21 | + suite.Suite |
| 22 | + |
| 23 | + chains map[uint64]struct{} |
| 24 | +} |
| 25 | + |
| 26 | +func TestRunUnlockHandlerTestSuite(t *testing.T) { |
| 27 | + suite.Run(t, new(UnlockHandlerTestSuite)) |
| 28 | +} |
| 29 | + |
| 30 | +func (s *UnlockHandlerTestSuite) SetupTest() { |
| 31 | + chains := make(map[uint64]struct{}) |
| 32 | + chains[1] = struct{}{} |
| 33 | + s.chains = chains |
| 34 | +} |
| 35 | + |
| 36 | +func (s *UnlockHandlerTestSuite) Test_HandleUnlock_InvalidRequest() { |
| 37 | + msgChn := make(chan []*message.Message) |
| 38 | + handler := handlers.NewUnlockHandler(msgChn, s.chains) |
| 39 | + |
| 40 | + input := handlers.UnlockBody{ |
| 41 | + Protocol: "lifi", |
| 42 | + OrderID: "id", |
| 43 | + Settler: "settler", |
| 44 | + } |
| 45 | + body, _ := json.Marshal(input) |
| 46 | + |
| 47 | + req := httptest.NewRequest(http.MethodPost, "/v1/chains/1/unlocks", bytes.NewReader(body)) |
| 48 | + req.Header.Set("Content-Type", "application/json") |
| 49 | + |
| 50 | + recorder := httptest.NewRecorder() |
| 51 | + |
| 52 | + go func() { |
| 53 | + msg := <-msgChn |
| 54 | + ad := msg[0].Data.(*across.LifiUnlockData) |
| 55 | + ad.SigChn <- signing.EcdsaSignature{} |
| 56 | + }() |
| 57 | + |
| 58 | + handler.HandleUnlock(recorder, req) |
| 59 | + |
| 60 | + s.Equal(http.StatusBadRequest, recorder.Code) |
| 61 | +} |
| 62 | + |
| 63 | +func (s *UnlockHandlerTestSuite) Test_HandleUnlock_InvalidProtocol() { |
| 64 | + msgChn := make(chan []*message.Message) |
| 65 | + handler := handlers.NewUnlockHandler(msgChn, s.chains) |
| 66 | + |
| 67 | + input := handlers.UnlockBody{ |
| 68 | + Protocol: "across", |
| 69 | + OrderID: "id", |
| 70 | + Settler: "settler", |
| 71 | + } |
| 72 | + body, _ := json.Marshal(input) |
| 73 | + |
| 74 | + req := httptest.NewRequest(http.MethodPost, "/v1/chains/1/unlocks", bytes.NewReader(body)) |
| 75 | + req.Header.Set("Content-Type", "application/json") |
| 76 | + req = mux.SetURLVars(req, map[string]string{ |
| 77 | + "chainId": "1", |
| 78 | + }) |
| 79 | + |
| 80 | + recorder := httptest.NewRecorder() |
| 81 | + |
| 82 | + go func() { |
| 83 | + msg := <-msgChn |
| 84 | + ad := msg[0].Data.(*across.LifiUnlockData) |
| 85 | + ad.SigChn <- signing.EcdsaSignature{} |
| 86 | + }() |
| 87 | + |
| 88 | + handler.HandleUnlock(recorder, req) |
| 89 | + |
| 90 | + s.Equal(http.StatusBadRequest, recorder.Code) |
| 91 | +} |
| 92 | + |
| 93 | +func (s *UnlockHandlerTestSuite) Test_HandleUnlock_ValidRequest() { |
| 94 | + msgChn := make(chan []*message.Message) |
| 95 | + handler := handlers.NewUnlockHandler(msgChn, s.chains) |
| 96 | + |
| 97 | + input := handlers.UnlockBody{ |
| 98 | + Protocol: "lifi", |
| 99 | + OrderID: "id", |
| 100 | + Settler: "settler", |
| 101 | + } |
| 102 | + body, _ := json.Marshal(input) |
| 103 | + |
| 104 | + req := httptest.NewRequest(http.MethodPost, "/v1/chains/1/unlocks", bytes.NewReader(body)) |
| 105 | + req.Header.Set("Content-Type", "application/json") |
| 106 | + req = mux.SetURLVars(req, map[string]string{ |
| 107 | + "chainId": "1", |
| 108 | + }) |
| 109 | + |
| 110 | + recorder := httptest.NewRecorder() |
| 111 | + |
| 112 | + sigBytes, _ := hex.DecodeString("abcd") |
| 113 | + go func() { |
| 114 | + msg := <-msgChn |
| 115 | + ad := msg[0].Data.(*across.LifiUnlockData) |
| 116 | + ad.SigChn <- signing.EcdsaSignature{ |
| 117 | + Signature: sigBytes, |
| 118 | + ID: "id", |
| 119 | + } |
| 120 | + }() |
| 121 | + |
| 122 | + handler.HandleUnlock(recorder, req) |
| 123 | + |
| 124 | + s.Equal(http.StatusOK, recorder.Code) |
| 125 | + data, err := io.ReadAll(recorder.Body) |
| 126 | + s.Nil(err) |
| 127 | + |
| 128 | + s.Equal(string(data), "{\"signature\":\"abcd\",\"id\":\"id\"}") |
| 129 | +} |
0 commit comments