|
114 | 114 | did.to_s.should == plc_did |
115 | 115 | end |
116 | 116 | end |
| 117 | + |
| 118 | + describe 'account status' do |
| 119 | + let(:document) { stub(:pds_endpoint => 'https://pds.ruby.space') } |
| 120 | + let(:did) { subject.new(plc_did) } |
| 121 | + |
| 122 | + before do |
| 123 | + did.stubs(:document).returns(document) |
| 124 | + |
| 125 | + stub_request(:get, 'https://pds.ruby.space/xrpc/com.atproto.sync.getRepoStatus') |
| 126 | + .with(query: { did: plc_did }) |
| 127 | + .to_return(http_response) if defined?(http_response) |
| 128 | + end |
| 129 | + |
| 130 | + context 'when repo is active' do |
| 131 | + let(:http_response) { |
| 132 | + { body: { active: true }.to_json, headers: { 'Content-Type' => 'application/json' }} |
| 133 | + } |
| 134 | + |
| 135 | + it 'should report active account state' do |
| 136 | + did.account_status.should == :active |
| 137 | + did.account_active?.should == true |
| 138 | + did.account_exists?.should == true |
| 139 | + end |
| 140 | + end |
| 141 | + |
| 142 | + context 'when repo is inactive' do |
| 143 | + let(:http_response) { |
| 144 | + { body: { active: false, status: 'takendown' }.to_json, headers: { 'Content-Type' => 'application/json' }} |
| 145 | + } |
| 146 | + |
| 147 | + it 'should report an inactive existing account' do |
| 148 | + did.account_status.should == :takendown |
| 149 | + did.account_active?.should == false |
| 150 | + did.account_exists?.should == true |
| 151 | + end |
| 152 | + end |
| 153 | + |
| 154 | + context 'when repo is not found' do |
| 155 | + let(:http_response) { |
| 156 | + { status: 400, body: { error: 'RepoNotFound' }.to_json, headers: { 'Content-Type' => 'application/json' }} |
| 157 | + } |
| 158 | + |
| 159 | + it 'should return nil status and report the account as missing' do |
| 160 | + did.account_status.should be_nil |
| 161 | + did.account_active?.should == false |
| 162 | + did.account_exists?.should == false |
| 163 | + end |
| 164 | + end |
| 165 | + |
| 166 | + context 'when the document has no pds endpoint' do |
| 167 | + before do |
| 168 | + did.stubs(:document).returns(stub(:pds_endpoint => nil)) |
| 169 | + end |
| 170 | + |
| 171 | + it 'should return nil status and report the account as missing' do |
| 172 | + did.account_status.should be_nil |
| 173 | + did.account_active?.should == false |
| 174 | + did.account_exists?.should == false |
| 175 | + end |
| 176 | + end |
| 177 | + |
| 178 | + context 'when active field is not set' do |
| 179 | + let(:http_response) { |
| 180 | + { body: { active: nil, status: 'unknown' }.to_json, headers: { 'Content-Type' => 'application/json' }} |
| 181 | + } |
| 182 | + |
| 183 | + it 'should raise APIError' do |
| 184 | + expect { did.account_status }.to raise_error(DIDKit::APIError) |
| 185 | + expect { did.account_active? }.to raise_error(DIDKit::APIError) |
| 186 | + expect { did.account_exists? }.to raise_error(DIDKit::APIError) |
| 187 | + end |
| 188 | + end |
| 189 | + |
| 190 | + context 'when active is false but status is not set' do |
| 191 | + let(:http_response) { |
| 192 | + { body: { active: false, status: nil }.to_json, headers: { 'Content-Type' => 'application/json' }} |
| 193 | + } |
| 194 | + |
| 195 | + it 'should raise APIError' do |
| 196 | + expect { did.account_status }.to raise_error(DIDKit::APIError) |
| 197 | + expect { did.account_active? }.to raise_error(DIDKit::APIError) |
| 198 | + expect { did.account_exists? }.to raise_error(DIDKit::APIError) |
| 199 | + end |
| 200 | + end |
| 201 | + |
| 202 | + context 'when an error different than RepoNotFound is returned' do |
| 203 | + let(:http_response) { |
| 204 | + { status: 400, body: { error: 'UserIsJerry' }.to_json, headers: { 'Content-Type' => 'application/json' }} |
| 205 | + } |
| 206 | + |
| 207 | + it 'should raise APIError' do |
| 208 | + expect { did.account_status }.to raise_error(DIDKit::APIError) |
| 209 | + expect { did.account_active? }.to raise_error(DIDKit::APIError) |
| 210 | + expect { did.account_exists? }.to raise_error(DIDKit::APIError) |
| 211 | + end |
| 212 | + end |
| 213 | + |
| 214 | + context 'when the response is not application/json' do |
| 215 | + let(:http_response) { |
| 216 | + { status: 400, body: 'error', headers: { 'Content-Type' => 'text/html' }} |
| 217 | + } |
| 218 | + |
| 219 | + it 'should raise APIError' do |
| 220 | + expect { did.account_status }.to raise_error(DIDKit::APIError) |
| 221 | + expect { did.account_active? }.to raise_error(DIDKit::APIError) |
| 222 | + expect { did.account_exists? }.to raise_error(DIDKit::APIError) |
| 223 | + end |
| 224 | + end |
| 225 | + |
| 226 | + context 'when the response is not 200 or 400' do |
| 227 | + let(:http_response) { |
| 228 | + { status: 500, body: { error: 'RepoNotFound' }.to_json, headers: { 'Content-Type' => 'application/json' }} |
| 229 | + } |
| 230 | + |
| 231 | + it 'should raise APIError' do |
| 232 | + expect { did.account_status }.to raise_error(DIDKit::APIError) |
| 233 | + expect { did.account_active? }.to raise_error(DIDKit::APIError) |
| 234 | + expect { did.account_exists? }.to raise_error(DIDKit::APIError) |
| 235 | + end |
| 236 | + end |
| 237 | + end |
117 | 238 | end |
0 commit comments