-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathsnapshots_controller_test.go
124 lines (95 loc) · 4.04 KB
/
snapshots_controller_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package web_test
import (
"bytes"
"encoding/json"
"testing"
"github.com/ethereum/go-ethereum/common"
"github.com/smartcontractkit/chainlink/store"
"github.com/smartcontractkit/chainlink/store/models"
"github.com/smartcontractkit/chainlink/utils"
"github.com/smartcontractkit/chainlink/internal/cltest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSnapshotsController_CreateSnapshot_V1_Format(t *testing.T) {
t.Parallel()
app, cleanup := cltest.NewApplication()
defer cleanup()
client := app.NewHTTPClient()
j := cltest.FixtureCreateJobWithAssignmentViaWeb(t, app, "../internal/fixtures/web/v1_format_job.json")
path := "/v1/assignments/" + j.ID + "/snapshots"
resp, cleanup := client.Post(path, bytes.NewBuffer([]byte{}))
defer cleanup()
runID := cltest.ParseCommonJSON(resp.Body).ID
assert.NotNil(t, runID)
}
func TestSnapshotsController_CreateSnapshot_V1_NotFound(t *testing.T) {
t.Parallel()
app, cleanup := cltest.NewApplication()
defer cleanup()
client := app.NewHTTPClient()
url := "/v1/assignments/" + "badid" + "/snapshots"
resp, cleanup := client.Post(url, bytes.NewBuffer([]byte{}))
defer cleanup()
assert.Equal(t, 404, resp.StatusCode, "Response should be not found")
}
func TestSnapshotsController_CreateSnapshot_V1_LateJob(t *testing.T) {
t.Parallel()
app, cleanup := cltest.NewApplication()
defer cleanup()
client := app.NewHTTPClient()
j := cltest.FixtureCreateJobWithAssignmentViaWeb(t, app, "../internal/fixtures/web/v1_format_job_past_endat_time.json")
url := "/v1/assignments/" + j.ID + "/snapshots"
resp, cleanup := client.Post(url, bytes.NewBuffer([]byte{}))
defer cleanup()
assert.Equal(t, 500, resp.StatusCode, "Response should be server error")
}
func TestSnapshotsController_ShowSnapshot_V1_Format(t *testing.T) {
t.Parallel()
tickerResponse := `{"high": "10744.00", "last": "10583.75", "timestamp": "1512156162", "bid": "10555.13", "vwap": "10097.98", "volume": "17861.33960013", "low": "9370.11", "ask": "10583.00", "open": "9927.29"}`
mockServer, assertCalled := cltest.NewHTTPMockServer(t, 200, "GET", tickerResponse)
defer assertCalled()
config, _ := cltest.NewConfigWithPrivateKey()
app, cleanup := cltest.NewApplicationWithConfigAndUnlockedAccount(config)
eth := app.MockEthClient()
newHeads := make(chan models.BlockHeader, 10)
eth.RegisterSubscription("newHeads", newHeads)
eth.Register("eth_getTransactionCount", `0x0100`)
hash := common.HexToHash("0xb7862c896a6ba2711bccc0410184e46d793ea83b3e05470f1d359ea276d16bb5")
sentAt := uint64(23456)
eth.Register("eth_blockNumber", utils.Uint64ToHex(sentAt))
eth.Register("eth_sendRawTransaction", hash)
eth.Register("eth_blockNumber", utils.Uint64ToHex(sentAt))
eth.Register("eth_getTransactionReceipt", store.TxReceipt{})
require.NoError(t, app.StartAndConnect())
defer cleanup()
client := app.NewHTTPClient()
j := cltest.CreateMockAssignmentViaWeb(t, app, mockServer.URL)
url := "/v1/assignments/" + j.ID + "/snapshots"
resp, cleanup := client.Post(url, bytes.NewBuffer([]byte{}))
defer cleanup()
assert.Equal(t, 200, resp.StatusCode, "Response should be successful")
runID := cltest.ParseCommonJSON(resp.Body).ID
cltest.WaitForRuns(t, j, app.Store, 1)
jr, err := app.Store.FindJobRun(runID)
assert.NoError(t, err)
jr = cltest.WaitForJobRunToPendConfirmations(t, app.Store, jr)
resp2, cleanup := client.Get("/v1/snapshots/" + runID)
defer cleanup()
assert.Equal(t, 200, resp2.StatusCode, "Response should be successful")
var ss models.Snapshot
assert.Nil(t, json.Unmarshal(cltest.ParseResponseBody(resp2), &ss))
assert.Equal(t, ss.ID, jr.Result.JobRunID)
assert.Equal(t, ss.Details.Result.String(), jr.Result.Data.Result.String())
assert.Empty(t, ss.Error.String)
assert.False(t, ss.Pending)
}
func TestSnapshotsController_ShowSnapshot_V1_NotFound(t *testing.T) {
t.Parallel()
app, cleanup := cltest.NewApplication()
defer cleanup()
client := app.NewHTTPClient()
resp, cleanup := client.Get("/v1/snapshots/" + "badid")
defer cleanup()
assert.Equal(t, 404, resp.StatusCode, "Response should be not found")
}