1# frozen_string_literal: true
  2
  3require "test_helper"
  4require "sip_account"
  5
  6class SipAccount
  7	attr_reader :password
  8end
  9
 10class SipAccountTest < Minitest::Test
 11	def setup
 12		@sip = SipAccount.new(
 13			BandwidthIris::SipCredential.new(
 14				user_name: "12345",
 15				realm: "sip.example.com",
 16				http_voice_v2_app_id: "sipappid"
 17			)
 18		).with(password: "old password")
 19	end
 20
 21	def test_with_random_password
 22		new_sip = @sip.with_random_password
 23		refute_equal @sip.password, new_sip.password
 24		refute_empty new_sip.password
 25		assert_kind_of String, new_sip.password
 26	end
 27
 28	def test_form
 29		form = @sip.form
 30		assert_equal "12345", form.field("username").value
 31		assert_equal "old password", form.field("password").value
 32		assert_equal "sip.example.com", form.field("server").value
 33	end
 34
 35	def test_put
 36		put = stub_request(
 37			:put,
 38			"https://dashboard.bandwidth.com/v1.0/accounts//sipcredentials/12345"
 39		).with(
 40			body: {
 41				Hash1: "73b05bcaf9096438c978aecff5f7cc45",
 42				Hash1b: "2b7fe68f6337ef4db29e752684a18db4",
 43				Realm: "sip.example.com",
 44				HttpVoiceV2AppId: "sipappid"
 45			}.to_xml(indent: 0, root: "SipCredential"),
 46			headers: {
 47				"Authorization" => "Basic Og=="
 48			}
 49		).to_return(
 50			status: 201,
 51			headers: { "Location" => "http://example.com/endpoint" }
 52		)
 53
 54		new_sip = @sip.put
 55		assert_equal "12345", new_sip.username
 56		assert_requested put
 57	end
 58	em :test_put
 59
 60	def test_put_fail
 61		stub_request(
 62			:put,
 63			"https://dashboard.bandwidth.com/v1.0/accounts//sipcredentials/12345"
 64		).to_return(status: 400)
 65		assert_raises(BandwidthIris::Errors::GenericError) { @sip.put }
 66	end
 67	em :test_put_fail
 68
 69	class NewTest < Minitest::Test
 70		def setup
 71			@sip = SipAccount.new(
 72				BandwidthIris::SipCredential.new(
 73					user_name: "12345",
 74					realm: "sip.example.com",
 75					http_voice_v2_app_id: "sipappid"
 76				)
 77			).with(password: "old password")
 78		end
 79
 80		def test_with_random_password
 81			new_sip = @sip.with_random_password
 82			refute_equal @sip.password, new_sip.password
 83			refute_empty new_sip.password
 84			assert_kind_of String, new_sip.password
 85		end
 86
 87		def test_put
 88			post = stub_request(
 89				:put,
 90				"https://dashboard.bandwidth.com/v1.0/accounts//sipcredentials/12345"
 91			).with(
 92				body: {
 93					Hash1: "73b05bcaf9096438c978aecff5f7cc45",
 94					Hash1b: "2b7fe68f6337ef4db29e752684a18db4",
 95					Realm: "sip.example.com",
 96					HttpVoiceV2AppId: "sipappid"
 97				}.to_xml(indent: 0, root: "SipCredential"),
 98				headers: {
 99					"Authorization" => "Basic Og=="
100				}
101			).to_return(status: 201)
102
103			new_sip = @sip.put
104			assert_equal "12345", new_sip.username
105			assert_requested post
106		end
107		em :test_put
108	end
109end