|
| 1 | +RSpec.describe OmniAuth::Strategies::Ethereum do |
| 2 | + let(:app) do |
| 3 | + Rack::Builder.new do |b| |
| 4 | + b.use Rack::Session::Cookie, :secret => 'abc123' |
| 5 | + b.use OmniAuth::Strategies::Ethereum |
| 6 | + b.run lambda { |_env| [200, {}, ['Not Found']] } |
| 7 | + end.to_app |
| 8 | + end |
| 9 | + |
| 10 | + before(:each) do |
| 11 | + allow(Time).to receive(:now).and_return( |
| 12 | + Time.at(1636680000, in: '+00:00')) |
| 13 | + end |
| 14 | + |
| 15 | + context 'request phase' do |
| 16 | + before(:each) { post '/auth/ethereum' } |
| 17 | + |
| 18 | + it 'displays a form' do |
| 19 | + expect(last_response.status).to eq(200) |
| 20 | + expect(last_response.body).to be_include('<form') |
| 21 | + end |
| 22 | + |
| 23 | + it 'has the callback as the action for the form' do |
| 24 | + expect(last_response.body).to be_include("action='/auth/ethereum/callback'") |
| 25 | + end |
| 26 | + |
| 27 | + it 'has a text field for each of the fields' do |
| 28 | + expect(last_response.body.scan('<input').size).to eq(3) |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + context 'callback phase' do |
| 33 | + let(:auth_hash) { last_request.env['omniauth.auth'] } |
| 34 | + let(:eth_message) { "Hello from Ruby! #{Time.now.utc.to_i}" } |
| 35 | + let(:eth_address) { '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266' } |
| 36 | + let(:eth_signature) { '0x362b5463470038a62c9d0652712539bab36f9c74914f5a05475a8e2e84a9719e396ab201e62d0c92da7de91a3a4a61a221c3f4e0b528b709ecdaa22ee8391d0f1c' } |
| 37 | + |
| 38 | + describe 'with varied nonces' do |
| 39 | + before do |
| 40 | + post '/auth/ethereum/callback', |
| 41 | + :eth_message => eth_message, |
| 42 | + :eth_address => eth_address, |
| 43 | + :eth_signature => eth_signature |
| 44 | + end |
| 45 | + |
| 46 | + context 'with nonce from too long ago' do |
| 47 | + let(:eth_message) { "Hello from Ruby! #{Time.now.utc.to_i - 11 * 60}" } |
| 48 | + |
| 49 | + it 'fails with invalid nonce' do |
| 50 | + expect(last_response.status).to eq(302) |
| 51 | + expect(last_response.location).to eq('/auth/failure?message=invalid_time&strategy=ethereum') |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + context 'with nonce from too far in future' do |
| 56 | + let(:eth_message) { "Hello from Ruby! #{Time.now.utc.to_i + 11 * 60}" } |
| 57 | + |
| 58 | + it 'fails with invalid nonce' do |
| 59 | + expect(last_response.status).to eq(302) |
| 60 | + expect(last_response.location).to eq('/auth/failure?message=invalid_time&strategy=ethereum') |
| 61 | + end |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + context 'with mismatching address' do |
| 66 | + before do |
| 67 | + post '/auth/ethereum/callback', |
| 68 | + :name => 'Example User', |
| 69 | + |
| 70 | + :eth_message => eth_message, |
| 71 | + :eth_address => '0xBADDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD', |
| 72 | + :eth_signature => '0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' |
| 73 | + end |
| 74 | + |
| 75 | + it 'fails with invalid credentials' do |
| 76 | + expect(last_response.status).to eq(302) |
| 77 | + expect(last_response.location).to eq('/auth/failure?message=invalid_credentials&strategy=ethereum') |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + context 'with default options' do |
| 82 | + before do |
| 83 | + post '/auth/ethereum/callback', |
| 84 | + :eth_message => eth_message, |
| 85 | + :eth_address => eth_address, |
| 86 | + :eth_signature => eth_signature |
| 87 | + end |
| 88 | + |
| 89 | + it 'sets the uid to the address' do |
| 90 | + expect(last_response.status).to eq(200) |
| 91 | + expect(auth_hash.uid).to eq(eth_address) |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + end |
| 96 | +end |
0 commit comments