|
| 1 | +# frozen_string_literal: true |
| 2 | + |
1 | 3 | require 'datadog/appsec/spec_helper' |
| 4 | +require 'datadog/appsec/contrib/support/devise_user_mock' |
| 5 | + |
2 | 6 | require 'datadog/appsec/contrib/devise/resource' |
3 | 7 | require 'datadog/appsec/contrib/devise/event' |
4 | 8 |
|
5 | 9 | RSpec.describe Datadog::AppSec::Contrib::Devise::Event do |
6 | 10 | let(:event) { described_class.new(resource, mode) } |
7 | 11 | let(:resource) { Datadog::AppSec::Contrib::Devise::Resource.new(object) } |
8 | 12 |
|
9 | | - let(:object_class) do |
10 | | - Class.new do |
11 | | - attr_reader :id, :uuid, :email, :username |
| 13 | + describe '#to_h' do |
| 14 | + context 'when resource is nil' do |
| 15 | + let(:event) { described_class.new(nil, 'identification') } |
12 | 16 |
|
13 | | - def initialize(id: nil, uuid: nil, email: nil, username: nil) |
14 | | - @id = id |
15 | | - @uuid = uuid |
16 | | - @email = email |
17 | | - @username = username |
18 | | - end |
| 17 | + it { expect(event.to_h).to eq({}) } |
19 | 18 | end |
20 | | - end |
21 | | - |
22 | | - context 'without resource' do |
23 | | - let(:resource) { nil } |
24 | | - let(:mode) { 'safe' } |
25 | | - |
26 | | - it do |
27 | | - expect(event.to_h).to eq({}) |
28 | | - end |
29 | | - end |
30 | | - |
31 | | - context 'safe mode' do |
32 | | - let(:mode) { 'safe' } |
33 | 19 |
|
34 | | - context 'with ID but not UUID' do |
35 | | - let(:object) { object_class.new(id: 1234) } |
| 20 | + context 'when mode is invalid' do |
| 21 | + let(:event) { described_class.new(resource, 'invalid') } |
| 22 | + let(:resource) { Datadog::AppSec::Contrib::Support::DeviseUserMock.new(id: 1234) } |
36 | 23 |
|
37 | | - it do |
38 | | - expect(event.user_id).to be_nil |
| 24 | + it 'writes warning log message' do |
| 25 | + expect(Datadog.logger).to receive(:warn) |
| 26 | + expect(event.to_h).to eq({}) |
39 | 27 | end |
40 | 28 | end |
41 | 29 |
|
42 | | - context 'with ID as UUID' do |
43 | | - let(:uuid) { '123e4567-e89b-12d3-a456-426655440000' } |
44 | | - let(:object) { object_class.new(uuid: uuid) } |
45 | | - |
46 | | - it do |
47 | | - expect(event.user_id).to eq(uuid) |
| 30 | + context 'when mode is identification and different resource attributes present' do |
| 31 | + let(:event) { described_class.new(resource, 'identification') } |
| 32 | + let(:resource) do |
| 33 | + Datadog:: AppSec:: Contrib:: Support:: DeviseUserMock.new(id: 1234, email: '[email protected]', username: 'John') |
48 | 34 | end |
| 35 | + |
| 36 | + it { expect(event.to_h).to eq({ email: '[email protected]', username: 'John' }) } |
49 | 37 | end |
50 | 38 | end |
51 | 39 |
|
52 | | - context 'extended mode' do |
53 | | - let(:mode) { 'extended' } |
| 40 | + describe '#user_id' do |
| 41 | + context 'when mode is anonymization and ID is not UUID-like' do |
| 42 | + let(:event) { described_class.new(resource, 'anonymization') } |
| 43 | + let(:resource) { Datadog::AppSec::Contrib::Support::DeviseUserMock.new(id: 1234) } |
54 | 44 |
|
55 | | - context 'ID' do |
56 | | - context 'with ID but not UUID' do |
57 | | - let(:object) { object_class.new(id: 1234) } |
58 | | - |
59 | | - it do |
60 | | - expect(event.user_id).to eq(1234) |
61 | | - end |
62 | | - end |
| 45 | + it { expect(event.user_id).to be_nil } |
| 46 | + end |
63 | 47 |
|
64 | | - context 'with ID as UUID' do |
65 | | - let(:uuid) { '123e4567-e89b-12d3-a456-426655440000' } |
66 | | - let(:object) { object_class.new(uuid: uuid) } |
| 48 | + context 'when mode is anonymization and ID is UUID-like' do |
| 49 | + let(:event) { described_class.new(resource, 'anonymization') } |
| 50 | + let(:resource) { Datadog::AppSec::Contrib::Support::DeviseUserMock.new(id: '00000000-0000-0000-0000-000000000000') } |
67 | 51 |
|
68 | | - it do |
69 | | - expect(event.user_id).to eq(uuid) |
70 | | - end |
71 | | - end |
| 52 | + it { expect(event.user_id).to eq('00000000-0000-0000-0000-000000000000') } |
72 | 53 | end |
73 | 54 |
|
74 | | - context 'Email and username' do |
75 | | - let(:object) { object_class.new(id: 1234, email: '[email protected]', username: 'John') } |
| 55 | + context 'when mode is identification and ID is not UUID-like' do |
| 56 | + let(:event) { described_class.new(resource, 'identification') } |
| 57 | + let(:resource) { Datadog::AppSec::Contrib::Support::DeviseUserMock.new(id: 1234) } |
76 | 58 |
|
77 | | - it do |
78 | | - expect(event.to_h).to eq({ email: '[email protected]', username: 'John' }) |
79 | | - end |
| 59 | + it { expect(event.user_id).to eq(1234) } |
80 | 60 | end |
81 | | - end |
82 | 61 |
|
83 | | - context 'invalid mode' do |
84 | | - let(:object) { object_class.new(id: 1234) } |
85 | | - let(:mode) { 'invalid' } |
| 62 | + context 'when mode is identification and ID is UUID-like' do |
| 63 | + let(:event) { described_class.new(resource, 'identification') } |
| 64 | + let(:resource) { Datadog::AppSec::Contrib::Support::DeviseUserMock.new(id: '00000000-0000-0000-0000-000000000000') } |
86 | 65 |
|
87 | | - it do |
88 | | - expect(Datadog.logger).to receive(:warn) |
89 | | - expect(event.to_h).to eq({}) |
| 66 | + it { expect(event.user_id).to eq('00000000-0000-0000-0000-000000000000') } |
90 | 67 | end |
91 | 68 | end |
92 | 69 | end |
0 commit comments