-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from Drieam/issue/15
Perform launch validation checks (#15)
- Loading branch information
Showing
9 changed files
with
175 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
class Nonce < ApplicationRecord | ||
belongs_to :tool, inverse_of: :nonces | ||
|
||
self.primary_key = :created_at | ||
|
||
validates :key, presence: true | ||
|
||
# Returns a boolean if the passed in string is used before | ||
# This check is handled by the unique constraint on the database | ||
def self.verify(tool, nonce) | ||
create!(tool: tool, key: nonce).persisted? | ||
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique | ||
false | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
class CreateNonces < ActiveRecord::Migration[6.0] | ||
def change | ||
create_table :nonces, id: false do |t| | ||
t.belongs_to :tool, null: false, foreign_key: true, index: false, type: :uuid | ||
|
||
t.string :key, null: false | ||
t.timestamps | ||
|
||
t.index %i[tool_id key], unique: true | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe Nonce, type: :model do | ||
describe 'database' do | ||
it { is_expected.to have_db_column(:tool_id).of_type(:uuid).with_options(null: false, foreign_key: true) } | ||
it { is_expected.to have_db_column(:key).of_type(:string).with_options(null: false) } | ||
it { is_expected.to have_db_column(:created_at).of_type(:datetime).with_options(null: false) } | ||
it { is_expected.to have_db_column(:updated_at).of_type(:datetime).with_options(null: false) } | ||
it { is_expected.to have_db_index(%i[tool_id key]).unique } | ||
it { is_expected.to_not have_db_index(:tool_id) } | ||
it { is_expected.to_not have_db_index(:key) } | ||
end | ||
|
||
describe 'relations' do | ||
it { is_expected.to belong_to(:tool).inverse_of(:nonces) } | ||
end | ||
|
||
describe 'validations' do | ||
it { is_expected.to validate_presence_of(:key) } | ||
end | ||
|
||
describe 'methods' do | ||
describe '.verify' do | ||
context 'without a tool' do | ||
it 'returns false' do | ||
expect(described_class.verify(nil, SecureRandom.hex)).to eq false | ||
end | ||
end | ||
context 'within a single tool' do | ||
let!(:tool) { create :tool } | ||
context 'with empty nonce' do | ||
it 'returns false' do | ||
expect(described_class.verify(tool, nil)).to eq false | ||
end | ||
end | ||
context 'with new nonce' do | ||
it 'it returns true' do | ||
expect(described_class.verify(tool, SecureRandom.hex)).to eq true | ||
end | ||
end | ||
context 'with used nonce' do | ||
let(:nonce) { SecureRandom.uuid } | ||
it 'it returns false' do | ||
described_class.verify(tool, nonce) | ||
expect(described_class.verify(tool, nonce)).to eq false | ||
end | ||
end | ||
end | ||
context 'with multiple tools' do | ||
let!(:tool1) { create :tool } | ||
let!(:tool2) { create :tool } | ||
let(:nonce) { SecureRandom.uuid } | ||
it 'can have the same nonce for multiple tools' do | ||
expect(described_class.verify(tool1, nonce)).to eq true | ||
expect(described_class.verify(tool2, nonce)).to eq true | ||
expect(described_class.verify(tool2, nonce)).to eq false | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters