forked from koraktor/steam-condenser-ruby
/
test_steam_id.rb
184 lines (133 loc) · 6.53 KB
/
test_steam_id.rb
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# This code is free software; you can redistribute it and/or modify it under
# the terms of the new BSD License.
#
# Copyright (c) 2011, Sebastian Staudt
require 'helper'
class TestSteamId < Test::Unit::TestCase
context 'The Community::SteamId class' do
should 'be able to resolve vanity URLs' do
Community::WebApi.expects(:json).
with('ISteamUser', 'ResolveVanityURL', 1, vanityurl: 'koraktor').
returns({ response: { success: 1, steamid: 76561197961384956 } })
steam_id64 = Community::SteamId.resolve_vanity_url 'koraktor'
assert_equal 76561197961384956, steam_id64
end
should 'be return nil when not able to resolve a vanity URL' do
Community::WebApi.expects(:json).
with('ISteamUser', 'ResolveVanityURL', 1, vanityurl: 'unknown').
returns({ response: { success: 42 } })
assert_nil Community::SteamId.resolve_vanity_url 'unknown'
end
should 'provide a conversion between 64bit Steam IDs and STEAM_IDs' do
steam_id = Community::SteamId.community_id_to_steam_id 76561197960290418
assert_equal 'STEAM_0:0:12345', steam_id
end
should 'provide a conversion between 64bit Steam IDs and STEAM_ID_3s' do
steam_id = Community::SteamId.community_id_to_steam_id3 76561197960497430
assert_equal '[U:1:231702]', steam_id
steam_id = Community::SteamId.community_id_to_steam_id3 76561197998273743
assert_equal '[U:1:38008015]', steam_id
steam_id = Community::SteamId.community_id_to_steam_id3 76561198000009691
assert_equal '[U:1:39743963]', steam_id
end
should 'provide a conversion between STEAM_IDs and 64bit Steam IDs' do
steam_id64 = Community::SteamId.steam_id_to_community_id 'STEAM_0:0:12345'
assert_equal 76561197960290418, steam_id64
end
should 'provide a conversion between U_IDs and 64bit Steam IDs' do
steam_id64 = Community::SteamId.steam_id_to_community_id '[U:1:12345]'
assert_equal 76561197960278073, steam_id64
steam_id64 = Community::SteamId.steam_id_to_community_id '[U:1:39743963]'
assert_equal 76561198000009691, steam_id64
end
end
context 'A Steam ID' do
should 'be correctly cached' do
assert_not Community::SteamId.cached? 76561197983311154
steam_id = Community::SteamId.new 76561197983311154, false
assert steam_id.cache
assert Community::SteamId.cached? 76561197983311154
end
should 'be correctly cached with its custom URL' do
assert_not Community::SteamId.cached? 'Son_of_Thor'
steam_id = Community::SteamId.new 'Son_of_Thor', false
assert steam_id.cache
assert Community::SteamId.cached? 'son_of_Thor'
end
should 'have an ID' do
steam_id1 = Community::SteamId.new 76561197983311154, false
steam_id2 = Community::SteamId.new 'Son_of_Thor', false
assert_equal 76561197983311154, steam_id1.id
assert_equal 'son_of_thor', steam_id2.id
end
should 'be able to fetch its data' do
url = fixture_io 'sonofthor.xml'
URI.expects(:open).with('http://steamcommunity.com/id/son_of_thor?xml=1', proxy: true).returns url
steam_id = Community::SteamId.new 'Son_of_Thor'
assert_equal 76561197983311154, steam_id.steam_id64
assert_equal 'son_of_thor', steam_id.custom_url
assert_equal 'Bellevue, Washington, United States', steam_id.location
assert_equal 'Son of Thor', steam_id.nickname
assert_equal 'Torsten Zabka', steam_id.real_name
assert_equal 'Last Online: 3 days ago', steam_id.state_message
assert_equal 'We jump that fence when we get to it.', steam_id.summary
assert_equal 'None', steam_id.trade_ban_state
assert_equal 'http://media.steampowered.com/steamcommunity/public/images/avatars/b8/b8438d91481295b7cc8da9578004cd63a2c3b2e4_full.jpg', steam_id.full_avatar_url
assert_equal 'http://media.steampowered.com/steamcommunity/public/images/avatars/b8/b8438d91481295b7cc8da9578004cd63a2c3b2e4.jpg', steam_id.icon_url
assert_equal 'http://media.steampowered.com/steamcommunity/public/images/avatars/b8/b8438d91481295b7cc8da9578004cd63a2c3b2e4_medium.jpg', steam_id.medium_avatar_url
assert_not steam_id.banned?
assert_not steam_id.limited?
assert_not steam_id.online?
assert steam_id.fetched?
assert steam_id.public?
end
should 'be able to query its Steam level' do
steam_id = Community::SteamId.new 76561197983311154, false
steam_id.expects(:update_steam_level).returns 10
assert_equal 10, steam_id.steam_level
end
should 'be able to return its Steam level' do
steam_id = Community::SteamId.new 76561197983311154, false
steam_id.instance_variable_set :@steam_level, 10
steam_id.expects(:update_steam_level).never
assert_equal 10, steam_id.steam_level
end
should 'be able to update its Steam level' do
steam_id = Community::SteamId.new 76561197983311154, false
Community::WebApi.expects(:json).
with('IPlayerService', 'GetSteamLevel', 1, steamid: 76561197983311154).
returns({ response: { player_level: 10 }})
assert_equal 10, steam_id.update_steam_level
assert_equal 10, steam_id.instance_variable_get(:@steam_level)
end
should 'be found by the 64bit Community::SteamId' do
steam_id = Community::SteamId.new 76561197983311154, false
assert_equal 76561197983311154, steam_id.steam_id64
assert_equal 'http://steamcommunity.com/profiles/76561197983311154', steam_id.base_url
end
should 'be found by the Community::SteamId\'s custom URL' do
steam_id = Community::SteamId.new 'Son_of_Thor', false
assert_equal 'son_of_thor', steam_id.custom_url
assert_equal 'http://steamcommunity.com/id/son_of_thor', steam_id.base_url
end
should 'raise an exception when parsing invalid XML' do
error = assert_raises Error do
url = fixture_io 'invalid.xml'
URI.expects(:open).with('http://steamcommunity.com/id/son_of_thor?xml=1', proxy: true).returns url
Community::SteamId.new 'Son_of_Thor'
end
assert_equal 'XML data could not be parsed.', error.message
end
should 'not cache an empty hash when an error is encountered on steam' do
Community::WebApi.expects(:json).raises SteamCondenser::Error::WebApi.new('tesst')
steam_id = Community::SteamId.new 76561197983311154, false
assert_raises SteamCondenser::Error::WebApi do
steam_id.games
end
assert_equal nil, steam_id.instance_variable_get(:@games)
end
teardown do
Community::SteamId.clear_cache
end
end
end