spec/rest/subusers/subusers_spec.rb in sendgrid4r-0.5.0 vs spec/rest/subusers/subusers_spec.rb in sendgrid4r-1.0.0

- old
+ new

@@ -4,22 +4,32 @@ describe SendGrid4r::REST::Subusers do describe 'integration test', :it do before do begin Dotenv.load - @client = SendGrid4r::Client.new( - username: ENV['SILVER_SENDGRID_USERNAME'], - password: ENV['SILVER_SENDGRID_PASSWORD']) + @client = SendGrid4r::Client.new(api_key: ENV['SILVER_API_KEY']) @username1 = ENV['SUBUSER1'] - @username2 = ENV['SILVER_SUBUSER'] - @username3 = ENV['SILVER_SUBUSER2'] - @email1 = ENV['SUBMAIL1'] - @password1 = ENV['SUBPASS1'] + @username2 = ENV['SUBUSER2'] + @username3 = ENV['SUBUSER3'] + @email1 = ENV['MAIL'] + @password1 = ENV['PASS'] @ip = @client.get_ips[0].ip # celan up test env - rescue => e + begin + @client.delete_subuser(username: @username3) + rescue => e + puts e.inspect + end + + @subuser3 = @client.post_subuser( + username: @username3, + email: @email1, + password: @password1, + ips: [@ip] + ) + rescue RestClient::ExceptionWithResponse => e puts e.inspect raise e end end @@ -29,119 +39,127 @@ subusers = @client.get_subusers(limit: 100, offset: 0) expect(subusers).to be_a(Array) subusers.each do |subuser| expect(subuser).to be_a(SendGrid4r::REST::Subusers::Subuser) end - rescue => e + rescue RestClient::ExceptionWithResponse => e puts e.inspect raise e end end it '#post_subuser' do begin - pending 'got RestClient::Forbidden:' - subuser2 = @client.post_subuser( - username: @username2, - email: @email2, - password: @password2, - ips: [@ip] + expect(@subuser3).to be_a(SendGrid4r::REST::Subusers::Subuser) + expect(@subuser3.username).to eq(@username3) + expect(@subuser3.email).to eq(@email1) + rescue RestClient::ExceptionWithResponse => e + puts e.inspect + raise e + end + end + + it '#patch_subuser' do + begin + @client.patch_subuser(username: @username3, disabled: true) + rescue RestClient::ExceptionWithResponse => e + puts e.inspect + raise e + end + end + + it '#delete_subuser' do + begin + @client.delete_subuser(username: @username3) + rescue RestClient::ExceptionWithResponse => e + puts e.inspect + raise e + end + end + + it '#get_subuser_monitor' do + begin + monitor = @client.get_subuser_monitor( + username: @username3, email: @email1, frequency: 10 ) - expect(subuser2).to be_a(SendGrid4r::REST::Subusers::Subuser) - expect(subuser2.username).to eq(@username2) - expect(subuser2.email).to eq(@email2) - expect(subuser2.password).to eq(@password2) - expect(subuser2.ips).to be_a(Array) - rescue => e + expect(monitor).to be_a(SendGrid4r::REST::Subusers::Monitor) + rescue RestClient::ExceptionWithResponse => e puts e.inspect raise e end end + it '#post_subuser_monitor' do + begin + monitor = @client.post_subuser_monitor( + username: @username3, email: @email1, frequency: 10 + ) + expect(monitor).to be_a(SendGrid4r::REST::Subusers::Monitor) + rescue RestClient::ExceptionWithResponse => e + puts e.inspect + raise e + end + end + + it '#put_subuser_monitor' do + begin + @client.post_subuser_monitor( + username: @username3, email: @email1, frequency: 10 + ) + monitor = @client.put_subuser_monitor( + username: @username3, email: @email1, frequency: 10 + ) + expect(monitor).to be_a(SendGrid4r::REST::Subusers::Monitor) + rescue RestClient::ExceptionWithResponse => e + puts e.inspect + raise e + end + end + + it '#delete_subuser_monitor' do + begin + @client.post_subuser_monitor( + username: @username3, email: @email1, frequency: 10 + ) + @client.delete_subuser_monitor(username: @username3) + rescue RestClient::ExceptionWithResponse => e + puts e.inspect + raise e + end + end + it '#get_subuser_reputation' do begin params = [] + params.push(@username1) params.push(@username2) - params.push(@username3) subusers = @client.get_subuser_reputation(usernames: params) expect(subusers).to be_a(Array) subusers.each do |subuser| expect(subuser).to be_a(SendGrid4r::REST::Subusers::Subuser) end - rescue => e + rescue RestClient::ExceptionWithResponse => e puts e.inspect raise e end end it '#put_subuser_assigned_ips' do begin - pending 'Invalid JSON' - subuser = @client.put_subuser_assigned_ips(username: @username2) + subuser = @client.put_subuser_assigned_ips( + username: @username2, ips: [@ip] + ) expect(subuser.ips).to be_a(Array) subuser.ips.each do |ip| expect(ip).to be_a(String) end - rescue => e + rescue RestClient::ExceptionWithResponse => e puts e.inspect raise e end end end - - context 'with block call' do - it '#get_subusers' do - @client.get_subusers(limit: 100, offset: 0) do |resp, req, res| - resp = - SendGrid4r::REST::Subusers.create_subusers(JSON.parse(resp)) - expect(resp).to be_a(Array) - expect(req).to be_a(RestClient::Request) - expect(res).to be_a(Net::HTTPOK) - end - end - - it '#post_subuser' do - pending 'got RestClient::Forbidden:' - @client.post_subuser( - username: @username2, - email: @email2, - password: @password2, - ips: [@ip] - ) do |resp, req, res| - resp = - SendGrid4r::REST::Subusers.create_subuser(JSON.parse(resp)) - expect(resp).to be_a(SendGrid4r::REST::Subusers::Subuser) - expect(req).to be_a(RestClient::Request) - expect(res).to be_a(Net::HTTPCreated) - end - end - - it '#get_subuser_reputation' do - @client.get_subuser_reputation( - usernames: [@username2] - ) do |resp, req, res| - resp = - SendGrid4r::REST::Subusers.create_subusers(JSON.parse(resp)) - expect(resp).to be_a(Array) - expect(req).to be_a(RestClient::Request) - expect(res).to be_a(Net::HTTPOK) - end - end - - it '#put_subuser_assigned_ips' do - pending 'Invalid JSON' - @client.put_subuser_assigned_ips( - username: @username2 - ) do |resp, req, res| - resp = - SendGrid4r::REST::Subusers.create_subuser(JSON.parse(resp)) - expect(resp).to be_a(SendGrid4r::REST::Subusers::Subuser) - expect(req).to be_a(RestClient::Request) - expect(res).to be_a(Net::HTTPOK) - end - end - end end describe 'unit test', :ut do let(:client) do SendGrid4r::Client.new(api_key: '') @@ -188,10 +206,19 @@ ']'\ '}' ) end + let(:monitor) do + JSON.parse( + '{'\ + '"email": "test@example.com",'\ + '"frequency": 500'\ + '}' + ) + end + it '#get_subusers' do allow(client).to receive(:execute).and_return(subusers) actual = client.get_subusers(limit: 0, offset: 0, username: 'aaa') expect(actual).to be_a(Array) actual.each do |subuser| @@ -205,23 +232,64 @@ username: '', email: '', password: '', ips: [] ) expect(actual).to be_a(SendGrid4r::REST::Subusers::Subuser) end + it '#patch_subuser' do + allow(client).to receive(:execute).and_return(subuser) + actual = client.patch_subuser(username: '', disabled: true) + expect(actual).to be_a(SendGrid4r::REST::Subusers::Subuser) + end + + it '#delete_subuser' do + allow(client).to receive(:execute).and_return('') + actual = client.delete_subuser(username: '') + expect(actual).to eq('') + end + + it '#get_subuser_monitor' do + allow(client).to receive(:execute).and_return(monitor) + actual = client.get_subuser_monitor( + username: '', email: '', frequency: 1 + ) + expect(actual).to be_a(SendGrid4r::REST::Subusers::Monitor) + end + + it '#post_subuser_monitor' do + allow(client).to receive(:execute).and_return(monitor) + actual = client.post_subuser_monitor( + username: '', email: '', frequency: 1 + ) + expect(actual).to be_a(SendGrid4r::REST::Subusers::Monitor) + end + + it '#put_subuser_monitor' do + allow(client).to receive(:execute).and_return(monitor) + actual = client.put_subuser_monitor( + username: '', email: '', frequency: 1 + ) + expect(actual).to be_a(SendGrid4r::REST::Subusers::Monitor) + end + + it '#delete_subuser_monitor' do + allow(client).to receive(:execute).and_return('') + actual = client.delete_subuser_monitor(username: '') + expect(actual).to eq('') + end + it '#get_subuser_reputation' do allow(client).to receive(:execute).and_return(subusers) actual = client.get_subuser_reputation(usernames: []) expect(actual).to be_a(Array) actual.each do |subuser| expect(subuser).to be_a(SendGrid4r::REST::Subusers::Subuser) end end it '#put_subuser_assigned_ips' do - pending 'waiting for sendgrid documentation update' allow(client).to receive(:execute).and_return(subuser) - actual = client.put_subuser_assigned_ips('', []) + actual = client.put_subuser_assigned_ips(username: '', ips: []) expect(actual).to be_a(SendGrid4r::REST::Subusers::Subuser) end it 'creates subusers instance' do actual = SendGrid4r::REST::Subusers.create_subusers(subusers) @@ -239,8 +307,15 @@ expect(actual.password).to eq('johns_password') actual.ips do |ip| expect(ip).to be_a(String) end expect(actual.disabled).to eq(false) + end + + it 'creates monitor instance' do + actual = SendGrid4r::REST::Subusers.create_monitor(monitor) + expect(actual).to be_a(SendGrid4r::REST::Subusers::Monitor) + expect(actual.email).to eq('test@example.com') + expect(actual.frequency).to eq(500) end end end