~singpolyma/sgx-jmp

ref: 3a278525ea8243c6073099490d23d079eb5fa255 sgx-jmp/test/test_sip_account.rb -rw-r--r-- 3.0 KiB
3a278525Stephen Paul Weber Create or reset SIP account 1 year, 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# frozen_string_literal: true

require "test_helper"
require "sip_account"

class SipAccount
	public :password, :url

	class New
		public :password
	end
end

class SipAccountTest < Minitest::Test
	def setup
		@sip = SipAccount.new(
			url: "https://api.catapult.inetwork.com/v1/" \
			     "users/catapult_user/domains/catapult_domain/endpoints/test",
			username: "12345",
			password: "old password"
		)
	end

	def test_with_random_password
		new_sip = @sip.with_random_password
		refute_equal @sip.password, new_sip.password
		refute_empty new_sip.password
		assert_kind_of String, new_sip.password
	end

	def test_form
		form = @sip.form
		assert_equal "12345", form.field("username").value
		assert_equal "old password", form.field("password").value
		assert_equal "host.bwapp.io.example.com", form.field("server").value
	end

	def test_put
		delete = stub_request(:delete, @sip.url).with(
			headers: {
				"Authorization" => "Basic Y2F0YXB1bHRfdG9rZW46Y2F0YXB1bHRfc2VjcmV0"
			}
		).to_return(status: 200)

		post = stub_request(
			:post,
			"https://api.catapult.inetwork.com/v1/users/" \
			"catapult_user/domains/catapult_domain/endpoints"
		).with(
			body: open(__dir__ + "/data/catapult_create_sip.json").read.chomp,
			headers: {
				"Authorization" => "Basic Y2F0YXB1bHRfdG9rZW46Y2F0YXB1bHRfc2VjcmV0",
				"Content-Type" => "application/json"
			}
		).to_return(
			status: 201,
			headers: { "Location" => "http://example.com/endpoint" }
		)

		new_sip = @sip.put.sync
		assert_equal "http://example.com/endpoint", new_sip.url
		assert_requested delete
		assert_requested post
	end
	em :test_put

	def test_put_delete_fail
		stub_request(:delete, @sip.url).to_return(status: 400)
		assert_raises(RuntimeError) { @sip.put.sync }
	end
	em :test_put_delete_fail

	def test_put_post_fail
		stub_request(:delete, @sip.url).to_return(status: 200)
		stub_request(
			:post,
			"https://api.catapult.inetwork.com/v1/users/" \
			"catapult_user/domains/catapult_domain/endpoints"
		).to_return(status: 400)
		assert_raises(RuntimeError) { @sip.put.sync }
	end
	em :test_put_post_fail

	class NewTest < Minitest::Test
		def setup
			@sip = SipAccount::New.new(
				username: "12345",
				password: "old password"
			)
		end

		def test_with_random_password
			new_sip = @sip.with_random_password
			refute_equal @sip.password, new_sip.password
			refute_empty new_sip.password
			assert_kind_of String, new_sip.password
		end

		def test_put
			post = stub_request(
				:post,
				"https://api.catapult.inetwork.com/v1/users/" \
				"catapult_user/domains/catapult_domain/endpoints"
			).with(
				body: open(__dir__ + "/data/catapult_create_sip.json").read.chomp,
				headers: {
					"Authorization" => "Basic Y2F0YXB1bHRfdG9rZW46Y2F0YXB1bHRfc2VjcmV0",
					"Content-Type" => "application/json"
				}
			).to_return(
				status: 201,
				headers: { "Location" => "http://example.com/endpoint" }
			)

			new_sip = @sip.put.sync
			assert_equal "http://example.com/endpoint", new_sip.url
			assert_requested post
		end
		em :test_put
	end
end