java_script_agent_spec.rb 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. require 'rails_helper'
  2. describe Agents::JavaScriptAgent do
  3. before do
  4. @valid_params = {
  5. :name => "somename",
  6. :options => {
  7. :code => "Agent.check = function() { this.createEvent({ 'message': 'hi' }); };",
  8. }
  9. }
  10. @agent = Agents::JavaScriptAgent.new(@valid_params)
  11. @agent.user = users(:jane)
  12. @agent.save!
  13. end
  14. describe "validations" do
  15. it "requires 'code'" do
  16. expect(@agent).to be_valid
  17. @agent.options['code'] = ''
  18. expect(@agent).not_to be_valid
  19. @agent.options.delete('code')
  20. expect(@agent).not_to be_valid
  21. end
  22. it "checks for a valid 'language', but allows nil" do
  23. expect(@agent).to be_valid
  24. @agent.options['language'] = ''
  25. expect(@agent).to be_valid
  26. @agent.options.delete('language')
  27. expect(@agent).to be_valid
  28. @agent.options['language'] = 'foo'
  29. expect(@agent).not_to be_valid
  30. %w[javascript JavaScript coffeescript CoffeeScript].each do |valid_language|
  31. @agent.options['language'] = valid_language
  32. expect(@agent).to be_valid
  33. end
  34. end
  35. it "accepts a credential, but it must exist" do
  36. expect(@agent).to be_valid
  37. @agent.options['code'] = 'credential:foo'
  38. expect(@agent).not_to be_valid
  39. users(:jane).user_credentials.create! :credential_name => "foo", :credential_value => "bar"
  40. expect(@agent.reload).to be_valid
  41. end
  42. end
  43. describe "#working?" do
  44. describe "when expected_update_period_in_days is set" do
  45. it "returns false when more than expected_update_period_in_days have passed since the last event creation" do
  46. @agent.options['expected_update_period_in_days'] = 1
  47. @agent.save!
  48. expect(@agent).not_to be_working
  49. @agent.check
  50. expect(@agent.reload).to be_working
  51. three_days_from_now = 3.days.from_now
  52. stub(Time).now { three_days_from_now }
  53. expect(@agent).not_to be_working
  54. end
  55. end
  56. describe "when expected_receive_period_in_days is set" do
  57. it "returns false when more than expected_receive_period_in_days have passed since the last event was received" do
  58. @agent.options['expected_receive_period_in_days'] = 1
  59. @agent.save!
  60. expect(@agent).not_to be_working
  61. Agents::JavaScriptAgent.async_receive @agent.id, [events(:bob_website_agent_event).id]
  62. expect(@agent.reload).to be_working
  63. two_days_from_now = 2.days.from_now
  64. stub(Time).now { two_days_from_now }
  65. expect(@agent.reload).not_to be_working
  66. end
  67. end
  68. end
  69. describe "executing code" do
  70. it "works by default" do
  71. @agent.options = @agent.default_options
  72. @agent.options['make_event'] = true
  73. @agent.save!
  74. expect {
  75. expect {
  76. @agent.receive([events(:bob_website_agent_event)])
  77. @agent.check
  78. }.not_to change { AgentLog.count }
  79. }.to change { Event.count }.by(2)
  80. end
  81. describe "using credentials as code" do
  82. before do
  83. @agent.user.user_credentials.create :credential_name => 'code-foo', :credential_value => 'Agent.check = function() { this.log("ran it"); };'
  84. @agent.options['code'] = "credential:code-foo\n\n"
  85. @agent.save!
  86. end
  87. it "accepts credentials" do
  88. @agent.check
  89. expect(AgentLog.last.message).to eq("ran it")
  90. end
  91. it "logs an error when the credential goes away" do
  92. @agent.user.user_credentials.delete_all
  93. @agent.reload.check
  94. expect(AgentLog.last.message).to eq("Unable to find credential")
  95. end
  96. end
  97. describe "error handling" do
  98. it "should log an error when V8 has issues" do
  99. @agent.options['code'] = 'syntax error!'
  100. @agent.save!
  101. expect {
  102. expect {
  103. @agent.check
  104. }.not_to raise_error
  105. }.to change { AgentLog.count }.by(1)
  106. expect(AgentLog.last.message).to match(/Unexpected identifier/)
  107. expect(AgentLog.last.level).to eq(4)
  108. end
  109. it "should log an error when JavaScript throws" do
  110. @agent.options['code'] = 'Agent.check = function() { throw "oh no"; };'
  111. @agent.save!
  112. expect {
  113. expect {
  114. @agent.check
  115. }.not_to raise_error
  116. }.to change { AgentLog.count }.by(1)
  117. expect(AgentLog.last.message).to match(/oh no/)
  118. expect(AgentLog.last.level).to eq(4)
  119. end
  120. end
  121. describe "getMemory" do
  122. it "won't store NaNs" do
  123. @agent.options['code'] = 'Agent.check = function() { this.memory("foo", NaN); };'
  124. @agent.save!
  125. @agent.check
  126. expect(@agent.memory['foo']).to eq('NaN') # string
  127. @agent.save!
  128. expect { @agent.reload.memory }.not_to raise_error
  129. end
  130. it "it stores an Array" do
  131. @agent.options['code'] = 'Agent.check = function() {
  132. var arr = [1,2];
  133. this.memory("foo", arr);
  134. };'
  135. @agent.save!
  136. @agent.check
  137. expect(@agent.memory['foo']).to eq([1,2])
  138. @agent.save!
  139. expect { @agent.reload.memory }.not_to raise_error
  140. end
  141. it "it stores a Hash" do
  142. @agent.options['code'] = 'Agent.check = function() {
  143. var obj = {};
  144. obj["one"] = 1;
  145. obj["two"] = [1,2];
  146. this.memory("foo", obj);
  147. };'
  148. @agent.save!
  149. @agent.check
  150. expect(@agent.memory['foo']).to eq({"one"=>1, "two"=> [1,2]})
  151. @agent.save!
  152. expect { @agent.reload.memory }.not_to raise_error
  153. end
  154. it "it stores a nested Hash" do
  155. @agent.options['code'] = 'Agent.check = function() {
  156. var u = {};
  157. u["one"] = 1;
  158. u["two"] = 2;
  159. var obj = {};
  160. obj["three"] = 3;
  161. obj["four"] = u;
  162. this.memory("foo", obj);
  163. };'
  164. @agent.save!
  165. @agent.check
  166. expect(@agent.memory['foo']).to eq({"three"=>3, "four"=>{"one"=>1, "two"=>2}})
  167. @agent.save!
  168. expect { @agent.reload.memory }.not_to raise_error
  169. end
  170. it "it stores null" do
  171. @agent.options['code'] = 'Agent.check = function() {
  172. this.memory("foo", "test");
  173. this.memory("foo", null);
  174. };'
  175. @agent.save!
  176. @agent.check
  177. expect(@agent.memory['foo']).to eq(nil)
  178. @agent.save!
  179. expect { @agent.reload.memory }.not_to raise_error
  180. end
  181. it "it stores false" do
  182. @agent.options['code'] = 'Agent.check = function() {
  183. this.memory("foo", "test");
  184. this.memory("foo", false);
  185. };'
  186. @agent.save!
  187. @agent.check
  188. expect(@agent.memory['foo']).to eq(false)
  189. @agent.save!
  190. expect { @agent.reload.memory }.not_to raise_error
  191. end
  192. end
  193. describe "setMemory" do
  194. it "stores an object" do
  195. @agent.options['code'] = 'Agent.check = function() {
  196. var u = {};
  197. u["one"] = 1;
  198. u["two"] = 2;
  199. this.setMemory(u);
  200. };'
  201. @agent.save!
  202. @agent.check
  203. expect(@agent.memory).to eq({ "one" => 1, "two" => 2 })
  204. @agent.save!
  205. expect { @agent.reload.memory }.not_to raise_error
  206. end
  207. end
  208. describe "deleteKey" do
  209. it "deletes a memory key" do
  210. @agent.memory = { foo: "baz" }
  211. @agent.options['code'] = 'Agent.check = function() {
  212. this.deleteKey("foo");
  213. };'
  214. @agent.save!
  215. @agent.check
  216. expect(@agent.memory['foo']).to be_nil
  217. expect { @agent.reload.memory }.not_to raise_error
  218. end
  219. it "returns the string value of the deleted key" do
  220. @agent.memory = { foo: "baz" }
  221. @agent.options['code'] = 'Agent.check = function() {
  222. this.createEvent({ message: this.deleteKey("foo")});
  223. };'
  224. @agent.save!
  225. @agent.check
  226. created_event = @agent.events.last
  227. expect(created_event.payload).to eq('message' => "baz")
  228. end
  229. it "returns the hash value of the deleted key" do
  230. @agent.memory = { foo: { baz: 'test' } }
  231. @agent.options['code'] = 'Agent.check = function() {
  232. this.createEvent({ message: this.deleteKey("foo")});
  233. };'
  234. @agent.save!
  235. @agent.check
  236. created_event = @agent.events.last
  237. expect(created_event.payload).to eq('message' => { 'baz' => 'test' })
  238. end
  239. end
  240. describe "creating events" do
  241. it "creates events with this.createEvent in the JavaScript environment" do
  242. @agent.options['code'] = 'Agent.check = function() { this.createEvent({ message: "This is an event!", stuff: { foo: 5 } }); };'
  243. @agent.save!
  244. expect {
  245. expect {
  246. @agent.check
  247. }.not_to change { AgentLog.count }
  248. }.to change { Event.count }.by(1)
  249. created_event = @agent.events.last
  250. expect(created_event.payload).to eq({ 'message' => "This is an event!", 'stuff' => { 'foo' => 5 } })
  251. end
  252. end
  253. describe "logging" do
  254. it "can output AgentLogs with this.log and this.error in the JavaScript environment" do
  255. @agent.options['code'] = 'Agent.check = function() { this.log("woah"); this.error("WOAH!"); };'
  256. @agent.save!
  257. expect {
  258. expect {
  259. @agent.check
  260. }.not_to raise_error
  261. }.to change { AgentLog.count }.by(2)
  262. log1, log2 = AgentLog.last(2)
  263. expect(log1.message).to eq("woah")
  264. expect(log1.level).to eq(3)
  265. expect(log2.message).to eq("WOAH!")
  266. expect(log2.level).to eq(4)
  267. end
  268. end
  269. describe "escaping and unescaping HTML" do
  270. it "can escape and unescape html with this.escapeHtml and this.unescapeHtml in the javascript environment" do
  271. @agent.options['code'] = 'Agent.check = function() { this.createEvent({ escaped: this.escapeHtml(\'test \"escaping\" <characters>\'), unescaped: this.unescapeHtml(\'test &quot;unescaping&quot; &lt;characters&gt;\')}); };'
  272. @agent.save!
  273. expect {
  274. expect {
  275. @agent.check
  276. }.not_to change { AgentLog.count }
  277. }.to change { Event.count}.by(1)
  278. created_event = @agent.events.last
  279. expect(created_event.payload).to eq({ 'escaped' => 'test &quot;escaping&quot; &lt;characters&gt;', 'unescaped' => 'test "unescaping" <characters>'})
  280. end
  281. end
  282. describe "getting incoming events" do
  283. it "can access incoming events in the JavaScript enviroment via this.incomingEvents" do
  284. event = Event.new
  285. event.agent = agents(:bob_rain_notifier_agent)
  286. event.payload = { :data => "Something you should know about" }
  287. event.save!
  288. event.reload
  289. @agent.options['code'] = <<-JS
  290. Agent.receive = function() {
  291. var events = this.incomingEvents();
  292. for(var i = 0; i < events.length; i++) {
  293. this.createEvent({ 'message': 'I got an event!', 'event_was': events[i].payload });
  294. }
  295. }
  296. JS
  297. @agent.save!
  298. expect {
  299. expect {
  300. @agent.receive([events(:bob_website_agent_event), event])
  301. }.not_to change { AgentLog.count }
  302. }.to change { Event.count }.by(2)
  303. created_event = @agent.events.first
  304. expect(created_event.payload).to eq({ 'message' => "I got an event!", 'event_was' => { 'data' => "Something you should know about" } })
  305. end
  306. end
  307. describe "getting and setting memory, getting options" do
  308. it "can access options via this.options and work with memory via this.memory" do
  309. @agent.options['code'] = <<-JS
  310. Agent.check = function() {
  311. if (this.options('make_event')) {
  312. var callCount = this.memory('callCount') || 0;
  313. this.memory('callCount', callCount + 1);
  314. }
  315. };
  316. JS
  317. @agent.save!
  318. expect {
  319. expect {
  320. @agent.check
  321. expect(@agent.memory['callCount']).not_to be_present
  322. @agent.options['make_event'] = true
  323. @agent.check
  324. expect(@agent.memory['callCount']).to eq(1)
  325. @agent.check
  326. expect(@agent.memory['callCount']).to eq(2)
  327. @agent.memory['callCount'] = 20
  328. @agent.check
  329. expect(@agent.memory['callCount']).to eq(21)
  330. }.not_to change { AgentLog.count }
  331. }.not_to change { Event.count }
  332. end
  333. end
  334. describe "using CoffeeScript" do
  335. it "will accept a 'language' of 'CoffeeScript'" do
  336. @agent.options['code'] = 'Agent.check = -> this.log("hello from coffeescript")'
  337. @agent.options['language'] = 'CoffeeScript'
  338. @agent.save!
  339. expect {
  340. @agent.check
  341. }.not_to raise_error
  342. expect(AgentLog.last.message).to eq("hello from coffeescript")
  343. end
  344. end
  345. describe "user credentials" do
  346. it "can access an existing credential" do
  347. @agent.send(:set_credential, 'test', 'hello')
  348. @agent.options['code'] = 'Agent.check = function() { this.log(this.credential("test")); };'
  349. @agent.save!
  350. @agent.check
  351. expect(AgentLog.last.message).to eq("hello")
  352. end
  353. it "will create a new credential" do
  354. @agent.options['code'] = 'Agent.check = function() { this.credential("test","1234"); };'
  355. @agent.save!
  356. expect {
  357. @agent.check
  358. }.to change(UserCredential, :count).by(1)
  359. end
  360. it "updates an existing credential" do
  361. @agent.send(:set_credential, 'test', 1234)
  362. @agent.options['code'] = 'Agent.check = function() { this.credential("test","12345"); };'
  363. @agent.save!
  364. expect {
  365. @agent.check
  366. }.to change(UserCredential, :count).by(0)
  367. expect(@agent.user.user_credentials.last.credential_value).to eq('12345')
  368. end
  369. end
  370. end
  371. end