From 509efd4ea191861fa94fb0e0041fd211bbfff0fc Mon Sep 17 00:00:00 2001 From: Kuba Suder Date: Sun, 28 Jun 2026 23:02:50 +0200 Subject: [PATCH] accept getRepoStatus returning responses with status 404 --- lib/didkit/did.rb | 18 +++++++++-------- spec/did_spec.rb | 50 +++++++++++++++++++++++++++++++---------------- 2 files changed, 43 insertions(+), 25 deletions(-) diff --git a/lib/didkit/did.rb b/lib/didkit/did.rb index 7a04e2a..ec0d150 100644 --- a/lib/didkit/did.rb +++ b/lib/didkit/did.rb @@ -148,17 +148,19 @@ module DIDKit json = JSON.parse(response.body) if json['active'] == true - :active + return :active elsif json['active'] == false && json['status'].is_a?(String) && json['status'].length <= 100 - json['status'].to_sym - else - raise APIError.new(response) + return json['status'].to_sym + end + elsif (status == 400 || status == 404) && is_json + json = JSON.parse(response.body) + + if json['error'] == 'RepoNotFound' + return nil end - elsif status == 400 && is_json && JSON.parse(response.body)['error'] == 'RepoNotFound' - nil - else - raise APIError.new(response) end + + raise APIError.new(response) end # Checks if the account is seen as active on its own PDS, using the `getRepoStatus` endpoint. diff --git a/spec/did_spec.rb b/spec/did_spec.rb index c335861..d7f42d3 100644 --- a/spec/did_spec.rb +++ b/spec/did_spec.rb @@ -163,6 +163,18 @@ describe DIDKit::DID do end end + context 'when repo is not found and we get a 404 response' do + let(:http_response) { + { status: 404, body: { error: 'RepoNotFound' }.to_json, headers: { 'Content-Type' => 'application/json' }} + } + + it 'should return nil status and report the account as missing' do + did.account_status.should be_nil + did.account_active?.should == false + did.account_exists?.should == false + end + end + context 'when the document has no pds endpoint' do before do did.stubs(:document).returns(stub(:pds_endpoint => nil)) @@ -199,31 +211,35 @@ describe DIDKit::DID do end end - context 'when an error different than RepoNotFound is returned' do - let(:http_response) { - { status: 400, body: { error: 'UserIsJerry' }.to_json, headers: { 'Content-Type' => 'application/json' }} - } + [400, 404].each do |status| + context "when an error different than RepoNotFound is returned with status #{status}" do + let(:http_response) { + { status: status, body: { error: 'UserIsJerry' }.to_json, headers: { 'Content-Type' => 'application/json' }} + } - it 'should raise APIError' do - expect { did.account_status }.to raise_error(DIDKit::APIError) - expect { did.account_active? }.to raise_error(DIDKit::APIError) - expect { did.account_exists? }.to raise_error(DIDKit::APIError) + it 'should raise APIError' do + expect { did.account_status }.to raise_error(DIDKit::APIError) + expect { did.account_active? }.to raise_error(DIDKit::APIError) + expect { did.account_exists? }.to raise_error(DIDKit::APIError) + end end end - context 'when the response is not application/json' do - let(:http_response) { - { status: 400, body: 'error', headers: { 'Content-Type' => 'text/html' }} - } + [200, 400, 404].each do |status| + context "when the response has status #{status} and is not application/json" do + let(:http_response) { + { status: status, body: 'error', headers: { 'Content-Type' => 'text/html' }} + } - it 'should raise APIError' do - expect { did.account_status }.to raise_error(DIDKit::APIError) - expect { did.account_active? }.to raise_error(DIDKit::APIError) - expect { did.account_exists? }.to raise_error(DIDKit::APIError) + it 'should raise APIError' do + expect { did.account_status }.to raise_error(DIDKit::APIError) + expect { did.account_active? }.to raise_error(DIDKit::APIError) + expect { did.account_exists? }.to raise_error(DIDKit::APIError) + end end end - context 'when the response is not 200 or 400' do + context 'when the response is not 200, 400, or 404' do let(:http_response) { { status: 500, body: { error: 'RepoNotFound' }.to_json, headers: { 'Content-Type' => 'application/json' }} } -- 2.51.2