diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/finders/user_emails_finder_spec.rb | 42 | ||||
-rw-r--r-- | spec/requests/api/users_spec.rb | 27 |
2 files changed, 66 insertions, 3 deletions
diff --git a/spec/finders/user_emails_finder_spec.rb b/spec/finders/user_emails_finder_spec.rb new file mode 100644 index 00000000000..b9f512b3a6b --- /dev/null +++ b/spec/finders/user_emails_finder_spec.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe UserEmailsFinder do + describe '#execute' do + let(:user) { create(:user) } + let!(:email1) { create(:email, user: user) } + let!(:email2) { create(:email, user: user) } + + def slice_email_attributes(emails) + emails.map { |email| email.slice('id', 'email') }.map(&:symbolize_keys) + end + + it 'returns the primary email with nil id when types is primary' do + emails = described_class.new(user.reload, types: %w[primary]).execute + + expect(slice_email_attributes(emails)).to eq([{ id: nil, email: user.email }]) + end + + it 'returns the secondary emails when types is secondary' do + emails = described_class.new(user.reload, types: %w[secondary]).execute + + expect(slice_email_attributes(emails)).to eq([{ id: email1.id, email: email1.email }, + { id: email2.id, email: email2.email }]) + end + + it 'returns both primary and secondary emails when type is primary,secondary sorted by id asc null first' do + emails = described_class.new(user.reload, types: %w[primary secondary]).execute + + expect(slice_email_attributes(emails)).to eq([{ id: nil, email: user.email }, + { id: email1.id, email: email1.email }, + { id: email2.id, email: email2.email }]) + end + + it 'returns an empty relation when types is empty' do + emails = described_class.new(user.reload, types: []).execute + + expect(emails).to be_empty + end + end +end diff --git a/spec/requests/api/users_spec.rb b/spec/requests/api/users_spec.rb index b84202364e1..0cea02fcceb 100644 --- a/spec/requests/api/users_spec.rb +++ b/spec/requests/api/users_spec.rb @@ -1146,18 +1146,39 @@ describe API::Users do expect(json_response['message']).to eq('404 User Not Found') end - it 'returns array of emails' do - user.emails << email - user.save + it 'returns array of secondary emails' do + email get api("/users/#{user.id}/emails", admin) expect(response).to have_gitlab_http_status(200) expect(response).to include_pagination_headers expect(json_response).to be_an Array + expect(json_response.length).to eq(1) expect(json_response.first['email']).to eq(email.email) end + it 'returns only the primary email when types[]=primary' do + email + + get api("/users/#{user.id}/emails", admin), params: { types: ['primary'] } + + expect(response).to have_gitlab_http_status(200) + expect(json_response.length).to eq(1) + expect(json_response.first).to eq({ 'email' => user.email }) + end + + it 'returns both primary and secondary emails when types[]=primary,secondary' do + email + + get api("/users/#{user.id}/emails", admin), params: { types: %w[primary secondary] } + + expect(response).to have_gitlab_http_status(200) + expect(json_response.length).to eq(2) + expect(json_response.first).to eq({ 'email' => user.email }) + expect(json_response.second).to eq({ 'id' => email.id, 'email' => email.email }) + end + it "returns a 404 for invalid ID" do get api("/users/ASDF/emails", admin) |