Sha256: 9c67e6225d108c4a524c48dc79bc506752a71c188a56caac708cb24ab16d969f

Contents?: true

Size: 1.63 KB

Versions: 4

Compression:

Stored size: 1.63 KB

Contents

require 'spec_helper'

describe ForeignKeyValidation::ModelExtension do

  let(:user) { User.create }
  let(:other_user) { User.create }

  context "with calling validation and missing foreign key on relation" do

    before do
      Issue.class_eval do
        validate_foreign_keys
      end
    end

    let(:project) { Project.create }
    let(:other_project) { Project.create }
    let(:issue) { Issue.create project: project, user: user }

    it "allow to rewrite user id of issue" do
      issue.user_id = other_user.id
      issue.save
      issue.reload
      expect(issue.user_id).to eq(other_user.id)
    end

    it "allow to rewrite project id of issue" do
      issue.project_id = other_project.id
      issue.save
      issue.reload
      expect(issue.project_id).to eq(other_project.id)
    end

  end

  context "with calling validation and missing foreign key on self" do

    before do
      Issue.class_eval do
        validate_foreign_keys
      end
    end

    let(:project) { Project.create user: user }
    let(:issue) { Issue.create project: project }

    it "does not allow to rewrite user id of issue" do
      issue.user_id = other_user.id
      issue.save
      expect(issue.errors.messages.values.flatten).to include("user_id of project does not match issues user_id.")
      expect(issue.reload.user_id).to_not eq(other_user.id)
    end

    it "does not allow to rewrite user id of issue with random id" do
      issue.user_id = 42
      issue.save
      expect(issue.errors.messages.values.flatten).to include("user_id of project does not match issues user_id.")
      expect(issue.reload.user_id).to_not eq(42)
    end

  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
foreign_key_validation-1.1.2 spec/model_extension/validation_with_missing_foreign_key_spec.rb
foreign_key_validation-1.1.1 spec/model_extension/validation_with_missing_foreign_key_spec.rb
foreign_key_validation-1.1.0 spec/model_extension/validation_with_missing_foreign_key_spec.rb
foreign_key_validation-1.0.1 spec/models/validation_with_missing_foreign_key_spec.rb