Verified Commit 94214896 authored by James Edwards-Jones's avatar James Edwards-Jones Committed by Paul Slaughter

PersonalAccessTokens can have associated projects

parent ca440758
......@@ -11,6 +11,9 @@ class PersonalAccessToken < ActiveRecord::Base
belongs_to :user
has_many :token_resources
has_many :projects, through: :token_resources
before_save :ensure_token
scope :active, -> { where("revoked = false AND (expires_at >= NOW() OR expires_at IS NULL)") }
......
class TokenResource < ActiveRecord::Base
belongs_to :personal_access_token
belongs_to :project
validates :personal_access_token, presence: true
validates :project, presence: true
end
class CreateTokenResources < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
def change
create_table :token_resources do |t|
t.belongs_to :personal_access_token, null: false, foreign_key: { on_delete: :cascade }, index: true
t.belongs_to :project, null: false, foreign_key: { on_delete: :cascade }, index: true
end
end
end
......@@ -2048,6 +2048,14 @@ ActiveRecord::Schema.define(version: 20181008145359) do
add_index "todos", ["user_id", "id"], name: "index_todos_on_user_id_and_id_pending", where: "((state)::text = 'pending'::text)", using: :btree
add_index "todos", ["user_id"], name: "index_todos_on_user_id", using: :btree
create_table "token_resources", force: :cascade do |t|
t.integer "personal_access_token_id", null: false
t.integer "project_id", null: false
end
add_index "token_resources", ["personal_access_token_id"], name: "index_token_resources_on_personal_access_token_id", using: :btree
add_index "token_resources", ["project_id"], name: "index_token_resources_on_project_id", using: :btree
create_table "trending_projects", force: :cascade do |t|
t.integer "project_id", null: false
end
......@@ -2441,6 +2449,8 @@ ActiveRecord::Schema.define(version: 20181008145359) do
add_foreign_key "todos", "projects", name: "fk_45054f9c45", on_delete: :cascade
add_foreign_key "todos", "users", column: "author_id", name: "fk_ccf0373936", on_delete: :cascade
add_foreign_key "todos", "users", name: "fk_d94154aa95", on_delete: :cascade
add_foreign_key "token_resources", "personal_access_tokens", on_delete: :cascade
add_foreign_key "token_resources", "projects", on_delete: :cascade
add_foreign_key "trending_projects", "projects", on_delete: :cascade
add_foreign_key "u2f_registrations", "users"
add_foreign_key "user_callouts", "users", on_delete: :cascade
......
......@@ -3,6 +3,13 @@ require 'spec_helper'
describe PersonalAccessToken do
subject { described_class }
describe 'associations' do
subject { described_class.new }
it { is_expected.to have_many(:token_resources) }
it { is_expected.to have_many(:projects) }
end
describe '.build' do
let(:personal_access_token) { build(:personal_access_token) }
let(:invalid_personal_access_token) { build(:personal_access_token, :invalid) }
......
require 'spec_helper'
describe TokenResource do
describe 'associations' do
it { is_expected.to belong_to(:personal_access_token) }
it { is_expected.to belong_to(:project) }
end
describe 'validations' do
it { is_expected.to validate_presence_of(:personal_access_token) }
it { is_expected.to validate_presence_of(:project) }
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment