-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract the apple key out from the config (#713)
* Extract the apple key out from the config * Remove unused * References the key attribute * Defer to the factory * Exclude the apple_keys table * Fix flickering test
- Loading branch information
Showing
12 changed files
with
135 additions
and
98 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,21 @@ | ||
# frozen_string_literal: true | ||
|
||
module Apple | ||
class Key < ApplicationRecord | ||
validates_presence_of :provider_id | ||
validates_presence_of :key_id | ||
validates_presence_of :key_pem_b64 | ||
|
||
validate :provider_id_is_valid, if: :provider_id? | ||
|
||
def provider_id_is_valid | ||
if provider_id.include?("_") | ||
errors.add(:provider_id, "cannot contain an underscore") | ||
end | ||
end | ||
|
||
def key_pem | ||
Base64.decode64(key_pem_b64) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
class ExtractCredentials < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :apple_keys do |t| | ||
t.string :provider_id | ||
t.string :key_id | ||
t.text :key_pem_b64 | ||
t.timestamps | ||
end | ||
|
||
add_reference :apple_configs, :key, index: true | ||
|
||
reversible do |direction| | ||
direction.up do | ||
Apple::Config.distinct.pluck(:apple_provider_id, :apple_key_id, :apple_key_pem_b64).each do |(apple_provider_id, apple_key_id, apple_key_pem_b64)| | ||
cred = Apple::Key.create!( | ||
provider_id: apple_provider_id, | ||
key_id: apple_key_id, | ||
key_pem_b64: apple_key_pem_b64 | ||
) | ||
|
||
Apple::Config.where(apple_provider_id: apple_provider_id, apple_key_id: apple_key_id, apple_key_pem_b64: apple_key_pem_b64).each do |config| | ||
config.update!(key_id: cred.id) | ||
end | ||
end | ||
end | ||
|
||
direction.down do | ||
Apple::Config.all.each do |config| | ||
config.update!( | ||
apple_key_id: config.key.key_id, | ||
apple_provider_id: config.key.provider_id, | ||
apple_key_pem_b64: config.key.key_pem_b64 | ||
) | ||
end | ||
end | ||
end | ||
|
||
remove_column :apple_configs, :apple_provider_id, :text | ||
remove_column :apple_configs, :apple_key_id, :text | ||
remove_column :apple_configs, :apple_key_pem_b64, :text | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,7 @@ | ||
FactoryBot.define do | ||
factory :key, class: Apple::Key, aliases: [:apple_key] do | ||
key_id { "some_key_id" } | ||
key_pem_b64 { Base64.encode64(test_file("/fixtures/apple_podcasts_connect_keyfile.pem")) } | ||
provider_id { SecureRandom.uuid } | ||
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,32 @@ | ||
require "test_helper" | ||
|
||
describe Apple::Config do | ||
describe "#valid?" do | ||
it "requires all apple credentials to have a value or be nil" do | ||
v1 = build(:apple_key, provider_id: nil, key_id: "blood", key_pem_b64: "orange") | ||
refute v1.valid? | ||
|
||
v2 = build(:apple_key, provider_id: "barlett", key_id: nil, key_pem_b64: "pear") | ||
refute v2.valid? | ||
|
||
v3 = build(:apple_key, provider_id: "cotton candy", key_id: "grapes", key_pem_b64: nil) | ||
refute v3.valid? | ||
|
||
v4 = build(:apple_key, provider_id: nil, key_id: nil, key_pem_b64: nil) | ||
refute v4.valid? | ||
end | ||
|
||
it "requires the apple provider id to not have an underscore" do | ||
v1 = build(:apple_key, provider_id: "foo_bar") | ||
refute v1.valid? | ||
assert_equal ["cannot contain an underscore"], v1.errors[:provider_id] | ||
end | ||
end | ||
|
||
describe "apple_key" do | ||
it "base64 decodes the apple key" do | ||
c = Apple::Key.new(key_pem_b64: Base64.encode64("hello")) | ||
assert_equal c.key_pem, "hello" | ||
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