activehistory 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,12 +10,12 @@ class EventTest < ActiveSupport::TestCase
10
10
  test 'Data captured from Event encalpsulation' do
11
11
  data = {
12
12
  ip: '127.0.0.1',
13
- user_agent: 'user-agent',
14
- session_id: 'session-id',
15
- account: 'model/id',
16
- api_key: 'api-key',
17
- metadata: {random: 'stuff'},
18
- timestamp: Time.now
13
+ user_agent: 'user-agent',
14
+ session_id: 'session-id',
15
+ performed_by_type: 'model',
16
+ performed_by_id: 'id',
17
+ metadata: {random: 'stuff'},
18
+ timestamp: Time.now
19
19
  }
20
20
 
21
21
  # TODO: timestamp: @attrs[:timestamp].iso8601(3),
@@ -24,13 +24,22 @@ class EventTest < ActiveSupport::TestCase
24
24
  create(:property)
25
25
  end
26
26
 
27
+ eid = nil
27
28
  assert_requested(:post, "http://activehistory.com/events", times: 1) do |req|
28
29
  req_data = JSON.parse(req.body)
29
30
  data.each do |k, v|
30
31
  assert_equal v.is_a?(Time) ? v.utc.iso8601(3) : v.as_json, req_data[k.to_s]
31
32
  end
32
33
 
33
- assert_equal 2, req_data['actions'].size
34
+ eid = req_data['id']
35
+ assert_equal 1, req_data['actions'].size
36
+ end
37
+
38
+ assert_requested(:post, "http://activehistory.com/actions", times: 1) do |req|
39
+ req_data = JSON.parse(req.body)
40
+
41
+ assert_equal 1, req_data['actions'].size
42
+ assert_equal eid, req_data['actions'].first['event_id']
34
43
  end
35
44
  end
36
45
 
@@ -56,4 +65,21 @@ class EventTest < ActiveSupport::TestCase
56
65
  assert_not_requested :any, /^http:\/\/activehistory.com\/.*/
57
66
  end
58
67
 
68
+ test 'Appending actions to an existing event' do
69
+ property = nil
70
+ ActiveHistory.encapsulate(id: "ddb666bd-73c8-4952-ab07-3a45e88f701b") {
71
+ property = create(:property)
72
+ }
73
+
74
+ assert_requested(:post, "http://activehistory.com/actions", times: 1) do |req|
75
+ req_data = JSON.parse(req.body)
76
+
77
+ assert_equal 1, req_data['actions'].size
78
+ req_data['actions'][0]['diff'].each do |k, v|
79
+ assert_equal property.send(k).is_a?(Time) ? property.send(k).utc.iso8601(3) : property.send(k).as_json, v[1]
80
+ end
81
+ end
82
+
83
+ end
84
+
59
85
  end
@@ -14,14 +14,12 @@ class SaveTest < ActiveSupport::TestCase
14
14
  @property.name = 'Empire State Building'
15
15
  travel_to(@time) { @property.save }
16
16
 
17
- assert_posted("/events") do |req|
18
- req_data = JSON.parse(req.body)
19
- assert_equal 1, req_data['actions'].size
20
-
21
- assert_equal req_data['actions'][0], {
17
+ assert_posted("/events") do
18
+ assert_action_for @property, {
22
19
  timestamp: @time.iso8601(3),
23
20
  type: 'update',
24
- subject: "Property/#{@property.id}",
21
+ subject_type: "Property",
22
+ subject_id: @property.id,
25
23
  diff: {
26
24
  name: ['unkown', 'Empire State Building']
27
25
  }
@@ -37,14 +35,12 @@ class SaveTest < ActiveSupport::TestCase
37
35
  @comment.body = 'new body'
38
36
  travel_to(@time) { @comment.save }
39
37
 
40
- assert_posted("/events") do |req|
41
- req_data = JSON.parse(req.body)
42
- assert_equal 1, req_data['actions'].size
43
-
44
- assert_equal req_data['actions'][0], {
38
+ assert_posted("/events") do
39
+ assert_action_for @comment, {
45
40
  timestamp: @time.iso8601(3),
46
41
  type: 'update',
47
- subject: "Comment/#{@comment.id}",
42
+ subject_type: "Comment",
43
+ subject_id: @comment.id,
48
44
  diff: {
49
45
  body: ['body', 'new body']
50
46
  }
@@ -0,0 +1,96 @@
1
+ require 'test_helper'
2
+
3
+ class ActiveHistoryTest < ActiveSupport::TestCase
4
+
5
+ def setup
6
+ super
7
+ @time = (Time.now.utc + 2).change(usec: 0)
8
+ end
9
+
10
+ test '::encapsulate(EVENT_ID) appends action to EVENT_ID'
11
+
12
+ test '::encapsulate(event) appends actions to the given event' do
13
+ @property = create(:property, name: 'unkown')
14
+ WebMock::RequestRegistry.instance.reset!
15
+
16
+ event = ActiveHistory::Event.create!
17
+
18
+ ActiveHistory.encapsulate(event) do
19
+ @property.name = 'Empire State Building'
20
+ travel_to(@time) { @property.save }
21
+ end
22
+
23
+ assert_posted('/actions') do |req|
24
+ assert_action_for @property, {
25
+ timestamp: @time.iso8601(3),
26
+ type: 'update',
27
+ event_id: event.id,
28
+ subject_type: 'Property',
29
+ subject_id: @property.id,
30
+ diff: { name: ['unkown', 'Empire State Building'] }
31
+ }.as_json
32
+ end
33
+
34
+ WebMock::RequestRegistry.instance.reset!
35
+
36
+ ActiveHistory.encapsulate(event) do
37
+ @property.name = 'Empire State'
38
+ travel_to(@time) { @property.save }
39
+ end
40
+
41
+ assert_posted('/actions') do |req|
42
+ assert_action_for @property, {
43
+ timestamp: @time.iso8601(3),
44
+ type: 'update',
45
+ event_id: event.id,
46
+ subject_type: 'Property',
47
+ subject_id: @property.id,
48
+ diff: { name: ['Empire State Building', 'Empire State'] }
49
+ }.as_json
50
+ end
51
+
52
+ end
53
+
54
+ test '::encapsulate' do
55
+ @property = create(:property, name: 'unkown')
56
+ WebMock::RequestRegistry.instance.reset!
57
+
58
+ ActiveHistory.encapsulate do
59
+ @property.name = 'Empire State Building'
60
+ travel_to(@time) { @property.save }
61
+
62
+ eid = nil
63
+ assert_posted("/events") do
64
+ eid = @req['id']
65
+ assert_action_for @property, {
66
+ timestamp: @time.iso8601(3),
67
+ type: 'update',
68
+ subject_type: "Property",
69
+ subject_id: @property.id,
70
+ diff: {
71
+ name: ['unkown', 'Empire State Building']
72
+ }
73
+ }.as_json
74
+ end
75
+
76
+ WebMock::RequestRegistry.instance.reset!
77
+ @property.name = "Crysler Building"
78
+ travel_to(@time + 1) { @property.save }
79
+
80
+ assert_posted("/actions") do
81
+ assert_action_for @property, {
82
+ event_id: eid,
83
+ timestamp: (@time + 1).iso8601(3),
84
+ type: 'update',
85
+ subject_type: "Property",
86
+ subject_id: @property.id,
87
+ diff: {
88
+ name: ['Empire State Building', 'Crysler Building']
89
+ }
90
+ }.as_json
91
+ end
92
+
93
+ end
94
+ end
95
+
96
+ end
@@ -12,7 +12,7 @@ class EmailAddress < ActiveRecord::Base
12
12
 
13
13
  track
14
14
 
15
- belongs_to :account
15
+ belongs_to :account, inverse_of: :email_address
16
16
 
17
17
  end
18
18
 
@@ -41,7 +41,18 @@ class ActiveSupport::TestCase
41
41
  end
42
42
 
43
43
  def assert_posted(path, &block)
44
- assert_requested(:post, "#{ActiveHistory.url}#{path}", times: 1, &block)
44
+ assert_requested(:post, "#{ActiveHistory.url}#{path}", times: 1) do |req|
45
+ @req = JSON.parse(req.body)
46
+ block.call @req
47
+ end
48
+ end
49
+
50
+ def assert_action_for(model, expected)
51
+ action = @req['actions'].find do |action|
52
+ action['subject_type'] == model.class.base_class.model_name.name && action['subject_id'] == model.id
53
+ end
54
+
55
+ assert_equal(expected.as_json, action)
45
56
  end
46
57
 
47
58
  def assert_not_posted(path)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activehistory
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: '0.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon Bracy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-05 00:00:00.000000000 Z
11
+ date: 2016-11-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -220,6 +220,20 @@ dependencies:
220
220
  - - '='
221
221
  - !ruby/object:Gem::Version
222
222
  version: 5.0.0.1
223
+ - !ruby/object:Gem::Dependency
224
+ name: globalid
225
+ requirement: !ruby/object:Gem::Requirement
226
+ requirements:
227
+ - - "~>"
228
+ - !ruby/object:Gem::Version
229
+ version: 0.3.7
230
+ type: :runtime
231
+ prerelease: false
232
+ version_requirements: !ruby/object:Gem::Requirement
233
+ requirements:
234
+ - - "~>"
235
+ - !ruby/object:Gem::Version
236
+ version: 0.3.7
223
237
  description: |
224
238
  ActiveHistory tracks and logs changes to your ActiveRecord models and
225
239
  relationships for auditing in the future.
@@ -252,6 +266,7 @@ files:
252
266
  - test/active_record_adapter/destroy_test.rb
253
267
  - test/active_record_adapter/event_test.rb
254
268
  - test/active_record_adapter/save_test.rb
269
+ - test/activehistory_test.rb
255
270
  - test/factories.rb
256
271
  - test/models.rb
257
272
  - test/schema.rb