Skip to content

Commit

Permalink
Save a reference to API key on deletion logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Undo1 committed Apr 25, 2017
1 parent bb1dbfe commit 4352499
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/controllers/api_controller.rb
Expand Up @@ -246,7 +246,7 @@ def post_deleted
end

post = Post.find params[:id]
dl = DeletionLog.new(:post => post, :is_deleted => true)
dl = post.deletion_logs.new(:api_key_id => @key.id, :is_deleted => true)

if dl.save
render :json => { :status => "success" }
Expand Down
1 change: 1 addition & 0 deletions app/models/api_key.rb
Expand Up @@ -6,6 +6,7 @@ class ApiKey < ApplicationRecord

has_many :feedbacks
has_many :api_tokens
has_many :deletion_logs

# ApiKey.user is the *owner* of the API application that the key belongs to.
belongs_to :user
Expand Down
1 change: 1 addition & 0 deletions app/models/deletion_log.rb
@@ -1,5 +1,6 @@
class DeletionLog < ApplicationRecord
belongs_to :post
belongs_to :deletion_log
validates :post_id, presence: true

after_create do
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20170425002546_add_api_key_id_to_deletion_logs.rb
@@ -0,0 +1,5 @@
class AddApiKeyIdToDeletionLogs < ActiveRecord::Migration[5.1]
def change
add_column :deletion_logs, :api_key_id, :integer
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170423142200) do
ActiveRecord::Schema.define(version: 20170425002546) do

create_table "api_keys", id: :integer, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.datetime "created_at", null: false
Expand Down Expand Up @@ -71,6 +71,7 @@
t.boolean "is_deleted"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "api_key_id"
t.index ["post_id"], name: "post_id_ix"
end

Expand Down
12 changes: 12 additions & 0 deletions test/controllers/api_controller_test.rb
Expand Up @@ -39,6 +39,18 @@ class ApiControllerTest < ActionController::TestCase
end
end

test "should associated deletion logs with API key" do
api_key = api_keys(:one)
api_key.update(is_trusted: true)

api_token = api_tokens(:one)

assert_difference 'api_key.deletion_logs.count' do
post :post_deleted, params: { id: 23653, key: api_keys(:one).key, token: api_tokens(:one).token }
assert_response :success
end
end

# This also happens to test that feedback is actually
# *created*, since expects delta=1
test "should prevent duplicate feedback from api" do
Expand Down

0 comments on commit 4352499

Please sign in to comment.