iachat/spec/jobs/account/contacts_export_job_spec.rb
Sojan Jose 8d8ec23aa1
chore: [Snyk] Security upgrade rspec-rails from 6.1.4 to 6.1.5 (#10066)
![snyk-top-banner](https://github.com/andygongea/OWASP-Benchmark/assets/818805/c518c423-16fe-447e-b67f-ad5a49b5d123)

### Snyk has created this PR to fix 1 vulnerabilities in the rubygems
dependencies of this project.

#### Snyk changed the following file(s):

- `Gemfile`



<details>
<summary>⚠️ <b>Warning</b></summary>

```
Failed to update the Gemfile.lock, please update manually before merging.
```

</details>



#### Vulnerabilities that will be fixed with an upgrade:

|  | Issue | Score | 

:-------------------------:|:-------------------------|:-------------------------
![medium
severity](https://res.cloudinary.com/snyk/image/upload/w_20,h_20/v1561977819/icon/m.png
'medium severity') | Web Cache Poisoning
<br/>[SNYK-RUBY-RACK-1061917](https://snyk.io/vuln/SNYK-RUBY-RACK-1061917)
| &nbsp;&nbsp;**616**&nbsp;&nbsp;




---

> [!IMPORTANT]
>
> - Check the changes in this PR to ensure they won't cause issues with
your project.
> - Max score is 1000. Note that the real score may have changed since
the PR was raised.
> - This PR was automatically created by Snyk using the credentials of a
real user.

---

**Note:** _You are seeing this because you or someone else with access
to this repository has authorized Snyk to open fix PRs._

For more information: <img
src="https://api.segment.io/v1/pixel/track?data=eyJ3cml0ZUtleSI6InJyWmxZcEdHY2RyTHZsb0lYd0dUcVg4WkFRTnNCOUEwIiwiYW5vbnltb3VzSWQiOiJhMWE2MzkzZS03ODdhLTRmYWItOGY1MS0zZjdmN2YzNzVlZDYiLCJldmVudCI6IlBSIHZpZXdlZCIsInByb3BlcnRpZXMiOnsicHJJZCI6ImExYTYzOTNlLTc4N2EtNGZhYi04ZjUxLTNmN2Y3ZjM3NWVkNiJ9fQ=="
width="0" height="0"/>
🧐 [View latest project
report](https://app.snyk.io/org/chatwoot/project/b7197bbd-6200-4f23-931d-c39928584360?utm_source&#x3D;github&amp;utm_medium&#x3D;referral&amp;page&#x3D;fix-pr)
📜 [Customise PR
templates](https://docs.snyk.io/scan-using-snyk/pull-requests/snyk-fix-pull-or-merge-requests/customize-pr-templates)
🛠 [Adjust project
settings](https://app.snyk.io/org/chatwoot/project/b7197bbd-6200-4f23-931d-c39928584360?utm_source&#x3D;github&amp;utm_medium&#x3D;referral&amp;page&#x3D;fix-pr/settings)
📚 [Read about Snyk's upgrade
logic](https://support.snyk.io/hc/en-us/articles/360003891078-Snyk-patches-to-fix-vulnerabilities)

---

**Learn how to fix vulnerabilities with free interactive lessons:**

🦉 [Learn about vulnerability in an interactive lesson of Snyk
Learn.](https://learn.snyk.io/?loc&#x3D;fix-pr)

[//]: #
'snyk:metadata:{"customTemplate":{"variablesUsed":[],"fieldsUsed":[]},"dependencies":[{"name":"rspec-rails","from":"6.1.4","to":"6.1.5"}],"env":"prod","issuesToFix":[{"exploit_maturity":"Proof
of
Concept","id":"SNYK-RUBY-RACK-1061917","priority_score":616,"priority_score_factors":[{"type":"exploit","label":"Proof
of
Concept","score":107},{"type":"fixability","label":true,"score":214},{"type":"cvssScore","label":"5.9","score":295},{"type":"scoreVersion","label":"v1","score":1}],"severity":"medium","title":"Web
Cache Poisoning"},{"exploit_maturity":"Proof of
Concept","id":"SNYK-RUBY-RACK-1061917","priority_score":616,"priority_score_factors":[{"type":"exploit","label":"Proof
of
Concept","score":107},{"type":"fixability","label":true,"score":214},{"type":"cvssScore","label":"5.9","score":295},{"type":"scoreVersion","label":"v1","score":1}],"severity":"medium","title":"Web
Cache Poisoning"},{"exploit_maturity":"Proof of
Concept","id":"SNYK-RUBY-RACK-1061917","priority_score":616,"priority_score_factors":[{"type":"exploit","label":"Proof
of
Concept","score":107},{"type":"fixability","label":true,"score":214},{"type":"cvssScore","label":"5.9","score":295},{"type":"scoreVersion","label":"v1","score":1}],"severity":"medium","title":"Web
Cache Poisoning"},{"exploit_maturity":"Proof of
Concept","id":"SNYK-RUBY-RACK-1061917","priority_score":616,"priority_score_factors":[{"type":"exploit","label":"Proof
of
Concept","score":107},{"type":"fixability","label":true,"score":214},{"type":"cvssScore","label":"5.9","score":295},{"type":"scoreVersion","label":"v1","score":1}],"severity":"medium","title":"Web
Cache
Poisoning"}],"prId":"a1a6393e-787a-4fab-8f51-3f7f7f375ed6","prPublicId":"a1a6393e-787a-4fab-8f51-3f7f7f375ed6","packageManager":"rubygems","priorityScoreList":[616],"projectPublicId":"b7197bbd-6200-4f23-931d-c39928584360","projectUrl":"https://app.snyk.io/org/chatwoot/project/b7197bbd-6200-4f23-931d-c39928584360?utm_source=github&utm_medium=referral&page=fix-pr","prType":"fix","templateFieldSources":{"branchName":"default","commitMessage":"default","description":"default","title":"default"},"templateVariants":["updated-fix-title","pr-warning-shown","priorityScore"],"type":"auto","upgrade":["SNYK-RUBY-RACK-1061917"],"vulns":["SNYK-RUBY-RACK-1061917"],"patch":[],"isBreakingChange":false,"remediationStrategy":"vuln"}'

---------

Co-authored-by: snyk-bot <snyk-bot@snyk.io>
2024-09-16 09:44:07 +05:30

130 lines
5.2 KiB
Ruby

require 'rails_helper'
RSpec.describe Account::ContactsExportJob do
subject(:job) { described_class.perform_later(account.id, user.id, [], {}) }
let(:account) { create(:account) }
let(:user) { create(:user, account: account, email: 'account-user-test@test.com') }
let(:label) { create(:label, title: 'spec-billing', maccount: account) }
let(:email_filter) do
{
:attribute_key => 'email',
:filter_operator => 'contains',
:values => 'looped',
:query_operator => 'and',
:attribute_model => 'standard',
:custom_attribute_type => ''
}
end
let(:city_filter) do
{
:attribute_key => 'country_code',
:filter_operator => 'equal_to',
:values => ['India'],
:query_operator => 'and',
:attribute_model => 'standard',
:custom_attribute_type => ''
}
end
let(:single_filter) do
{
:payload => [email_filter.merge(:query_operator => nil)]
}
end
let(:multiple_filters) do
{
:payload => [city_filter, email_filter.merge(:query_operator => nil)]
}
end
it 'enqueues the job' do
expect { job }.to have_enqueued_job(described_class)
.on_queue('low')
end
context 'when export_contacts' do
before do
create(:contact, account: account, phone_number: '+910808080818', email: 'test1@text.example')
4.times do |i|
create(:contact, account: account, email: "looped-#{i + 3}@text.example.com")
end
4.times do |i|
create(:contact, account: account, additional_attributes: { :country_code => 'India' }, email: "looped-#{i + 10}@text.example.com")
end
create(:contact, account: account, phone_number: '+910808080808', email: 'test2@text.example')
end
it 'generates CSV file and attach to account' do
mailer = double
allow(AdministratorNotifications::ChannelNotificationsMailer).to receive(:with).with(account: account).and_return(mailer)
allow(mailer).to receive(:contact_export_complete)
described_class.perform_now(account.id, user.id, [], {})
file_url = Rails.application.routes.url_helpers.rails_blob_url(account.contacts_export)
expect(account.contacts_export).to be_present
expect(file_url).to be_present
expect(mailer).to have_received(:contact_export_complete).with(file_url, user.email)
end
it 'generates valid data export file' do
described_class.perform_now(account.id, user.id, %w[id name email phone_number column_not_present], {})
csv_data = CSV.parse(account.contacts_export.download, headers: true)
emails = csv_data.pluck('email')
phone_numbers = csv_data.pluck('phone_number')
expect(csv_data.length).to eq(account.contacts.count)
expect(emails).to include('test1@text.example', 'test2@text.example')
expect(phone_numbers).to include('+910808080818', '+910808080808')
end
it 'returns all resolved contacts as results when filter is not prvoided' do
create(:contact, account: account, email: nil, phone_number: nil)
described_class.perform_now(account.id, user.id, %w[id name email column_not_present], {})
csv_data = CSV.parse(account.contacts_export.download, headers: true)
expect(csv_data.length).to eq(account.contacts.resolved_contacts.count)
end
it 'returns resolved contacts filtered if labels are provided' do
# Adding label to a resolved contact
Contact.last.add_labels(['spec-billing'])
contact = create(:contact, account: account, email: nil, phone_number: nil)
contact.add_labels(['spec-billing'])
described_class.perform_now(account.id, user.id, [], { :payload => nil, :label => 'spec-billing' })
csv_data = CSV.parse(account.contacts_export.download, headers: true)
# since there is only 1 resolved contact with 'spec-billing' label
expect(csv_data.length).to eq(1)
end
# TODO: This returns unresolved contacts as well since filter service returns the same
# Change this when we make changes to filter service and ensure only resolved contacts are returned
it 'returns filtered data which inclues unresolved contacts when filter is provided' do
create(:contact, account: account, email: nil, phone_number: nil, additional_attributes: { :country_code => 'India' })
described_class.perform_now(account.id, user.id, [], { :payload => [city_filter.merge(:query_operator => nil)] }.with_indifferent_access)
csv_data = CSV.parse(account.contacts_export.download, headers: true)
expect(csv_data.length).to eq(5)
end
it 'returns filtered data when multiple filters are provided' do
described_class.perform_now(account.id, user.id, [], multiple_filters.with_indifferent_access)
csv_data = CSV.parse(account.contacts_export.download, headers: true)
# since there are only 4 contacts with 'looped' in email and 'India' as country_code
expect(csv_data.length).to eq(4)
end
it 'returns filtered data when a single filter is provided' do
described_class.perform_now(account.id, user.id, [], single_filter.with_indifferent_access)
csv_data = CSV.parse(account.contacts_export.download, headers: true)
# since there are only 8 contacts with 'looped' in email
expect(csv_data.length).to eq(8)
end
end
end