/
TezosPlatformDiscoveryOperationsTest.scala
267 lines (227 loc) · 14.3 KB
/
TezosPlatformDiscoveryOperationsTest.scala
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package tech.cryptonomic.conseil.tezos
import java.sql.Timestamp
import java.time.LocalDateTime
import cats.effect.{ContextShift, IO}
import cats.effect.concurrent.MVar
import com.typesafe.scalalogging.LazyLogging
import org.scalamock.scalatest.MockFactory
import org.scalatest.concurrent.{IntegrationPatience, ScalaFutures}
import org.scalatest.{Matchers, OptionValues, WordSpec}
import slick.dbio
import tech.cryptonomic.conseil.config.Newest
import tech.cryptonomic.conseil.generic.chain.MetadataOperations
import tech.cryptonomic.conseil.generic.chain.PlatformDiscoveryTypes._
import tech.cryptonomic.conseil.tezos.FeeOperations.AverageFees
import tech.cryptonomic.conseil.tezos.TezosPlatformDiscoveryOperations.{AttributesCache, EntitiesCache}
import tech.cryptonomic.conseil.util.ConfigUtil
import scala.concurrent.ExecutionContext
import scala.concurrent.duration._
class TezosPlatformDiscoveryOperationsTest
extends WordSpec
with InMemoryDatabase
with MockFactory
with Matchers
with ScalaFutures
with OptionValues
with IntegrationPatience
with LazyLogging {
import slick.jdbc.PostgresProfile.api._
import tech.cryptonomic.conseil.config.Platforms._
import scala.concurrent.ExecutionContext.Implicits.global
val metadataOperations: MetadataOperations = new MetadataOperations {
override def runQuery[A](action: dbio.DBIO[A]) = dbHandler.run(action)
}
implicit val contextShift: ContextShift[IO] = IO.contextShift(implicitly[ExecutionContext])
val attributesCache = MVar[IO].empty[AttributesCache].unsafeRunSync()
val entitiesCache = MVar[IO].empty[EntitiesCache].unsafeRunSync()
val sut = TezosPlatformDiscoveryOperations(metadataOperations, attributesCache, entitiesCache, 10 seconds)
override def beforeAll(): Unit = {
super.beforeAll()
sut.init()
}
"getNetworks" should {
"return list with one element" in {
val config = PlatformsConfiguration(
platforms = Map(
Tezos -> List(TezosConfiguration("alphanet", TezosNodeConfiguration(protocol = "http", hostname = "localhost", port = 8732)))
)
)
ConfigUtil.getNetworks(config, "tezos") shouldBe List(Network("alphanet", "Alphanet", "tezos", "alphanet"))
}
"return two networks" in {
val config = PlatformsConfiguration(
platforms = Map(
Tezos -> List(
TezosConfiguration(
"alphanet",
TezosNodeConfiguration(protocol = "http", hostname = "localhost", port = 8732)
),
TezosConfiguration(
"alphanet-staging",
TezosNodeConfiguration(protocol = "https", hostname = "nautilus.cryptonomic.tech", port = 8732, pathPrefix = "tezos/alphanet/")
)
)
)
)
ConfigUtil.getNetworks(config, "tezos") should have size 2
}
}
"getEntities" should {
"return list of attributes of Fees" in {
sut.getTableAttributes("fees").futureValue shouldBe
Some(
List(
Attribute("low", "Low", DataType.Int, None, KeyType.NonKey, "fees"),
Attribute("medium", "Medium", DataType.Int, None, KeyType.NonKey, "fees"),
Attribute("high", "High", DataType.Int, None, KeyType.NonKey, "fees"),
Attribute("timestamp", "Timestamp", DataType.DateTime, None, KeyType.NonKey, "fees"),
Attribute("kind", "Kind", DataType.String, Some(0), KeyType.NonKey, "fees")
)
)
}
"return list of attributes of accounts" in {
sut.getTableAttributes("accounts").futureValue shouldBe
Some(
List(
Attribute("account_id", "Account id", DataType.String, Some(0), KeyType.UniqueKey, "accounts"),
Attribute("block_id", "Block id", DataType.String, Some(0), KeyType.NonKey, "accounts"),
Attribute("manager", "Manager", DataType.String, Some(0), KeyType.UniqueKey, "accounts"),
Attribute("spendable", "Spendable", DataType.Boolean, Some(0), KeyType.NonKey, "accounts"),
Attribute("delegate_setable", "Delegate setable", DataType.Boolean, Some(0), KeyType.NonKey, "accounts"),
Attribute("delegate_value", "Delegate value", DataType.String, Some(0), KeyType.NonKey, "accounts"),
Attribute("counter", "Counter", DataType.Int, None, KeyType.NonKey, "accounts"),
Attribute("script", "Script", DataType.String, Some(0), KeyType.NonKey, "accounts"),
Attribute("balance", "Balance", DataType.Decimal, None, KeyType.NonKey, "accounts"),
Attribute("block_level", "Block level", DataType.Decimal, None, KeyType.UniqueKey, "accounts")
)
)
}
"return list of attributes of blocks" in {
sut.getTableAttributes("blocks").futureValue shouldBe
Some(
List(
Attribute("level", "Level", DataType.Int, None, KeyType.UniqueKey, "blocks"),
Attribute("proto", "Proto", DataType.Int, None, KeyType.NonKey, "blocks"),
Attribute("predecessor", "Predecessor", DataType.String, Some(0), KeyType.NonKey, "blocks"),
Attribute("timestamp", "Timestamp", DataType.DateTime, None, KeyType.NonKey, "blocks"),
Attribute("validation_pass", "Validation pass", DataType.Int,None, KeyType.NonKey, "blocks"),
Attribute("fitness", "Fitness", DataType.String, Some(0), KeyType.NonKey, "blocks"),
Attribute("context", "Context", DataType.String, Some(0), KeyType.NonKey, "blocks"),
Attribute("signature", "Signature", DataType.String, Some(0), KeyType.NonKey, "blocks"),
Attribute("protocol", "Protocol", DataType.String, Some(0), KeyType.NonKey, "blocks"),
Attribute("chain_id", "Chain id", DataType.String, Some(0), KeyType.NonKey, "blocks"),
Attribute("hash", "Hash", DataType.String, Some(0), KeyType.UniqueKey, "blocks"),
Attribute("operations_hash", "Operations hash", DataType.String, Some(0), KeyType.NonKey, "blocks"),
Attribute("period_kind", "Period kind", DataType.String, Some(0), KeyType.NonKey, "blocks"),
Attribute("current_expected_quorum", "Current expected quorum", DataType.Int, None, KeyType.NonKey, "blocks"),
Attribute("active_proposal", "Active proposal", DataType.String, Some(0), KeyType.NonKey, "blocks"),
Attribute("baker", "Baker", DataType.String, Some(0), KeyType.NonKey, "blocks"),
Attribute("nonce_hash", "Nonce hash", DataType.String, Some(0), KeyType.NonKey, "blocks"),
Attribute("consumed_gas", "Consumed gas", DataType.Decimal, None, KeyType.NonKey, "blocks"),
Attribute("meta_level", "Meta level", DataType.Int, None, KeyType.NonKey, "blocks"),
Attribute("meta_level_position", "Meta level position", DataType.Int, None, KeyType.NonKey, "blocks"),
Attribute("meta_cycle", "Meta cycle", DataType.Int, None, KeyType.NonKey, "blocks"),
Attribute("meta_cycle_position", "Meta cycle position", DataType.Int, None, KeyType.NonKey, "blocks"),
Attribute("meta_voting_period", "Meta voting period", DataType.Int, None, KeyType.NonKey, "blocks"),
Attribute("meta_voting_period_position", "Meta voting period position", DataType.Int, None, KeyType.NonKey, "blocks"),
Attribute("expected_commitment", "Expected commitment", DataType.Boolean, Some(0), KeyType.NonKey, "blocks")
)
)
}
"return list of attributes of operations" in {
sut.getTableAttributes("operations").futureValue shouldBe
Some(
List(
Attribute("operation_id", "Operation id", DataType.Int, None, KeyType.UniqueKey, "operations"),
Attribute("operation_group_hash", "Operation group hash", DataType.String, Some(0), KeyType.NonKey, "operations"),
Attribute("kind", "Kind", DataType.String, Some(0), KeyType.NonKey, "operations"),
Attribute("level", "Level", DataType.Int, None, KeyType.NonKey, "operations"),
Attribute("delegate", "Delegate", DataType.String, Some(0), KeyType.NonKey, "operations"),
Attribute("slots", "Slots", DataType.String, Some(0), KeyType.NonKey, "operations"),
Attribute("nonce", "Nonce", DataType.String, Some(0), KeyType.NonKey, "operations"),
Attribute("pkh", "Pkh", DataType.String, Some(0), KeyType.NonKey, "operations"),
Attribute("secret", "Secret", DataType.String, Some(0), KeyType.NonKey, "operations"),
Attribute("source", "Source", DataType.String, Some(0), KeyType.UniqueKey, "operations"),
Attribute("fee", "Fee", DataType.Decimal, None, KeyType.NonKey, "operations"),
Attribute("counter", "Counter", DataType.Decimal, None, KeyType.NonKey, "operations"),
Attribute("gas_limit", "Gas limit", DataType.Decimal, None, KeyType.NonKey, "operations"),
Attribute("storage_limit", "Storage limit", DataType.Decimal, None, KeyType.NonKey, "operations"),
Attribute("public_key", "Public key", DataType.String, Some(0), KeyType.NonKey, "operations"),
Attribute("amount", "Amount", DataType.Decimal, None, KeyType.NonKey, "operations"),
Attribute("destination", "Destination", DataType.String, Some(0), KeyType.UniqueKey, "operations"),
Attribute("parameters", "Parameters", DataType.String, Some(0), KeyType.NonKey, "operations"),
Attribute("manager_pubkey", "Manager pubkey", DataType.String, Some(0), KeyType.NonKey, "operations"),
Attribute("balance", "Balance", DataType.Decimal, None, KeyType.NonKey, "operations"),
Attribute("spendable", "Spendable", DataType.Boolean, Some(0), KeyType.NonKey, "operations"),
Attribute("delegatable", "Delegatable", DataType.Boolean, Some(0), KeyType.NonKey, "operations"),
Attribute("script", "Script", DataType.String, Some(0), KeyType.NonKey, "operations"),
Attribute("storage", "Storage", DataType.String, Some(0), KeyType.NonKey, "operations"),
Attribute("status", "Status", DataType.String, Some(0), KeyType.NonKey, "operations"),
Attribute("consumed_gas", "Consumed gas", DataType.Decimal, None, KeyType.NonKey, "operations"),
Attribute("storage_size", "Storage size", DataType.Decimal, None, KeyType.NonKey, "operations"),
Attribute("paid_storage_size_diff", "Paid storage size diff", DataType.Decimal, None, KeyType.NonKey, "operations"),
Attribute("block_hash", "Block hash", DataType.String, Some(0), KeyType.NonKey, "operations"),
Attribute("block_level", "Block level", DataType.Int, None, KeyType.NonKey, "operations"),
Attribute("timestamp", "Timestamp", DataType.DateTime, None, KeyType.NonKey, "operations")
)
)
}
"return list of attributes of operation groups" in {
sut.getTableAttributes("operation_groups").futureValue shouldBe
Some(
List(
Attribute("protocol", "Protocol", DataType.String, Some(0), KeyType.NonKey, "operation_groups"),
Attribute("chain_id", "Chain id", DataType.String, Some(0), KeyType.NonKey, "operation_groups"),
Attribute("hash", "Hash", DataType.String, Some(0), KeyType.UniqueKey, "operation_groups"),
Attribute("branch", "Branch", DataType.String, Some(0), KeyType.NonKey, "operation_groups"),
Attribute("signature", "Signature", DataType.String, Some(0), KeyType.NonKey, "operation_groups"),
Attribute("block_id", "Block id", DataType.String, Some(0), KeyType.NonKey, "operation_groups")
)
)
}
"return empty list for non existing table" in {
sut.getTableAttributes("nonExisting").futureValue shouldBe None
}
}
"listAttributeValues" should {
"return list of values of kind attribute of Fees without filter" in {
val avgFee = AverageFees(1, 3, 5, Timestamp.valueOf(LocalDateTime.of(2018, 11, 22, 12, 30)), "example1")
metadataOperations.runQuery(TezosDatabaseOperations.writeFees(List(avgFee))).isReadyWithin(5.seconds)
sut.listAttributeValues("fees", "kind", None).futureValue shouldBe List("example1")
}
"returns a failed future when asked for medium attribute of Fees without filter - numeric attributes should not be displayed" in {
val avgFee = AverageFees(1, 3, 5, Timestamp.valueOf(LocalDateTime.of(2018, 11, 22, 12, 30)), "example1")
dbHandler.run(TezosDatabaseOperations.writeFees(List(avgFee))).isReadyWithin(5.seconds)
intercept[NoSuchElementException] {
throw sut.listAttributeValues("fees", "medium", None).failed.futureValue
}
}
"return empty list when trying to sql inject" in {
val avgFee = AverageFees(1, 3, 5, Timestamp.valueOf(LocalDateTime.of(2018, 11, 22, 12, 30)), "example1")
dbHandler.run(TezosDatabaseOperations.writeFees(List(avgFee))).isReadyWithin(5.seconds)
// That's how the SLQ-injected string will look like:
// SELECT DISTINCT kind FROM fees WHERE kind LIKE '%'; DELETE FROM fees WHERE kind LIKE '%'
val maliciousFilter = Some("'; DELETE FROM fees WHERE kind LIKE '")
sut.listAttributeValues("fees", "kind", maliciousFilter).futureValue shouldBe List.empty
dbHandler.run(Tables.Fees.length.result).futureValue shouldBe 1
}
"correctly apply the filter" in {
val avgFees = List(
AverageFees(1, 3, 5, Timestamp.valueOf(LocalDateTime.of(2018, 11, 22, 12, 30)), "example1"),
AverageFees(2, 4, 6, Timestamp.valueOf(LocalDateTime.of(2018, 11, 22, 12, 31)), "example2")
)
sut.listAttributeValues("fees", "kind", Some("1")).futureValue shouldBe List.empty
dbHandler.run(TezosDatabaseOperations.writeFees(avgFees)).isReadyWithin(5.seconds)
sut.listAttributeValues("fees", "kind", None).futureValue should contain theSameElementsAs List("example1", "example2")
sut.listAttributeValues("fees", "kind", Some("ex")).futureValue should contain theSameElementsAs List("example1", "example2")
sut.listAttributeValues("fees", "kind", Some("ample")).futureValue should contain theSameElementsAs List("example1", "example2")
sut.listAttributeValues("fees", "kind", Some("1")).futureValue shouldBe List("example1")
}
"should validate correctly fields" in {
sut.isAttributeValid("fees", "low").futureValue shouldBe true
}
"should return false when there will be field not existing in the DB" in {
sut.isAttributeValid("fees", "WRONG").futureValue shouldBe false
}
}
}