|
| 1 | +require 'spec_helper' |
| 2 | +require 'urbanairship' |
| 3 | +require 'urbanairship/devices/attributes' |
| 4 | + |
| 5 | +describe Urbanairship::Devices::Attributes do |
| 6 | + let(:key_1) { 'first_name' } |
| 7 | + let(:value_1) { 'Airship' } |
| 8 | + let(:key_2) { 'last_name' } |
| 9 | + let(:value_2) { 'API' } |
| 10 | + let(:timestamp) { Time.now.utc - 3600 } |
| 11 | + |
| 12 | + let(:attributes) do |
| 13 | + [ |
| 14 | + { key: key_1, value: value_1, action: described_class::SET, timestamp: timestamp }, |
| 15 | + ] |
| 16 | + end |
| 17 | + let(:expected) do |
| 18 | + { |
| 19 | + attributes: [ |
| 20 | + { |
| 21 | + key: key_1, |
| 22 | + value: value_1, |
| 23 | + action: described_class::SET, |
| 24 | + timestamp: timestamp.iso8601, |
| 25 | + }, |
| 26 | + ], |
| 27 | + } |
| 28 | + end |
| 29 | + |
| 30 | + before { Timecop.freeze(Time.now.utc) } |
| 31 | + after { Timecop.return } |
| 32 | + |
| 33 | + describe 'Payload' do |
| 34 | + let(:payload) { described_class.new(attributes).payload } |
| 35 | + |
| 36 | + context 'Action type is not set' do |
| 37 | + let(:attributes) do |
| 38 | + [ |
| 39 | + { key: key_1, value: value_1, timestamp: timestamp }, |
| 40 | + ] |
| 41 | + end |
| 42 | + |
| 43 | + it 'defaults to `set` action' do |
| 44 | + puts timestamp.iso8601 |
| 45 | + expect(payload).to eq expected |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + describe 'Set action' do |
| 50 | + it 'defaults to `set` action' do |
| 51 | + expect(payload).to eq expected |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + context 'Timestamp not provided' do |
| 56 | + let(:timestamp) { nil } |
| 57 | + let(:expected) do |
| 58 | + { |
| 59 | + attributes: [ |
| 60 | + { |
| 61 | + key: key_1, |
| 62 | + value: value_1, |
| 63 | + action: described_class::SET, |
| 64 | + timestamp: Time.now.utc.iso8601, |
| 65 | + }, |
| 66 | + ], |
| 67 | + } |
| 68 | + end |
| 69 | + |
| 70 | + it 'uses expected provided timestamp' do |
| 71 | + expect(payload).to eq expected |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + describe 'Remove action' do |
| 76 | + let(:attributes) do |
| 77 | + [ |
| 78 | + { key: key_1, action: described_class::REMOVE, timestamp: timestamp } |
| 79 | + ] |
| 80 | + end |
| 81 | + let(:expected) do |
| 82 | + { |
| 83 | + attributes: [ |
| 84 | + { |
| 85 | + key: key_1, |
| 86 | + action: described_class::REMOVE, |
| 87 | + timestamp: timestamp.iso8601, |
| 88 | + }, |
| 89 | + ], |
| 90 | + } |
| 91 | + end |
| 92 | + |
| 93 | + it 'generates expected payload' do |
| 94 | + expect(payload).to eq expected |
| 95 | + end |
| 96 | + end |
| 97 | + |
| 98 | + describe 'Loops over multiple records' do |
| 99 | + let(:attributes) do |
| 100 | + [ |
| 101 | + { key: key_1, action: described_class::REMOVE }, |
| 102 | + { key: key_2, value: value_2 } |
| 103 | + ] |
| 104 | + end |
| 105 | + let(:expected) do |
| 106 | + { |
| 107 | + attributes: [ |
| 108 | + { |
| 109 | + key: key_1, |
| 110 | + action: described_class::REMOVE, |
| 111 | + timestamp: Time.now.utc.iso8601, |
| 112 | + }, |
| 113 | + { |
| 114 | + key: key_2, |
| 115 | + value: value_2, |
| 116 | + action: described_class::SET, |
| 117 | + timestamp: Time.now.utc.iso8601, |
| 118 | + }, |
| 119 | + ], |
| 120 | + } |
| 121 | + end |
| 122 | + |
| 123 | + it 'generates expected payload' do |
| 124 | + expect(payload).to eq expected |
| 125 | + end |
| 126 | + end |
| 127 | + end |
| 128 | +end |
0 commit comments