Commit 5b35000f authored by Andrew8xx8's avatar Andrew8xx8

Snippets feature refactored. Tests now use spinach

parent bc7c5f87
...@@ -27,12 +27,12 @@ class Projects::SnippetsController < Projects::ApplicationController ...@@ -27,12 +27,12 @@ class Projects::SnippetsController < Projects::ApplicationController
end end
def create def create
@snippet = @project.snippets.new(params[:snippet]) @snippet = @project.snippets.new(params[:project_snippet])
@snippet.author = current_user @snippet.author = current_user
@snippet.save @snippet.save
if @snippet.valid? if @snippet.valid?
redirect_to [@project, @snippet] redirect_to project_snippet_path(@project, @snippet)
else else
respond_with(@snippet) respond_with(@snippet)
end end
...@@ -42,10 +42,10 @@ class Projects::SnippetsController < Projects::ApplicationController ...@@ -42,10 +42,10 @@ class Projects::SnippetsController < Projects::ApplicationController
end end
def update def update
@snippet.update_attributes(params[:snippet]) @snippet.update_attributes(params[:project_snippet])
if @snippet.valid? if @snippet.valid?
redirect_to [@project, @snippet] redirect_to project_snippet_path(@project, @snippet)
else else
respond_with(@snippet) respond_with(@snippet)
end end
...@@ -58,7 +58,7 @@ class Projects::SnippetsController < Projects::ApplicationController ...@@ -58,7 +58,7 @@ class Projects::SnippetsController < Projects::ApplicationController
end end
def destroy def destroy
return access_denied! unless can?(current_user, :admin_snippet, @snippet) return access_denied! unless can?(current_user, :admin_project_snippet, @snippet)
@snippet.destroy @snippet.destroy
...@@ -81,11 +81,11 @@ class Projects::SnippetsController < Projects::ApplicationController ...@@ -81,11 +81,11 @@ class Projects::SnippetsController < Projects::ApplicationController
end end
def authorize_modify_snippet! def authorize_modify_snippet!
return render_404 unless can?(current_user, :modify_snippet, @snippet) return render_404 unless can?(current_user, :modify_project_snippet, @snippet)
end end
def authorize_admin_snippet! def authorize_admin_snippet!
return render_404 unless can?(current_user, :admin_snippet, @snippet) return render_404 unless can?(current_user, :admin_project_snippet, @snippet)
end end
def module_enabled def module_enabled
......
...@@ -60,7 +60,7 @@ class SnippetsController < ProjectResourceController ...@@ -60,7 +60,7 @@ class SnippetsController < ProjectResourceController
@snippet.destroy @snippet.destroy
redirect_to project_snippets_path(@project) redirect_to project_snippet_path(@project)
end end
def raw def raw
......
...@@ -7,6 +7,7 @@ class Ability ...@@ -7,6 +7,7 @@ class Ability
when "Project" then project_abilities(user, subject) when "Project" then project_abilities(user, subject)
when "Issue" then issue_abilities(user, subject) when "Issue" then issue_abilities(user, subject)
when "Note" then note_abilities(user, subject) when "Note" then note_abilities(user, subject)
when "ProjectSnippet" then project_snippet_abilities(user, subject)
when "Snippet" then snippet_abilities(user, subject) when "Snippet" then snippet_abilities(user, subject)
when "MergeRequest" then merge_request_abilities(user, subject) when "MergeRequest" then merge_request_abilities(user, subject)
when "Group", "Namespace" then group_abilities(user, subject) when "Group", "Namespace" then group_abilities(user, subject)
...@@ -135,7 +136,7 @@ class Ability ...@@ -135,7 +136,7 @@ class Ability
end end
[:issue, :note, :snippet, :merge_request].each do |name| [:issue, :note, :project_snippet, :snippet, :merge_request].each do |name|
define_method "#{name}_abilities" do |user, subject| define_method "#{name}_abilities" do |user, subject|
if subject.author == user if subject.author == user
[ [
......
...@@ -50,7 +50,7 @@ class Project < ActiveRecord::Base ...@@ -50,7 +50,7 @@ class Project < ActiveRecord::Base
has_many :milestones, dependent: :destroy has_many :milestones, dependent: :destroy
has_many :users_projects, dependent: :destroy has_many :users_projects, dependent: :destroy
has_many :notes, dependent: :destroy has_many :notes, dependent: :destroy
has_many :snippets, dependent: :destroy has_many :snippets, dependent: :destroy, class_name: "ProjectSnippet"
has_many :deploy_keys, dependent: :destroy, class_name: "Key", foreign_key: "project_id" has_many :deploy_keys, dependent: :destroy, class_name: "Key", foreign_key: "project_id"
has_many :hooks, dependent: :destroy, class_name: "ProjectHook" has_many :hooks, dependent: :destroy, class_name: "ProjectHook"
has_many :wikis, dependent: :destroy has_many :wikis, dependent: :destroy
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
= @snippet.new_record? ? "New Snippet" : "Edit Snippet ##{@snippet.id}" = @snippet.new_record? ? "New Snippet" : "Edit Snippet ##{@snippet.id}"
%hr %hr
.snippet-form-holder .snippet-form-holder
= form_for [@project, @snippet] do |f| = form_for [@project, @snippet], as: :project_snippet, url: url do |f|
-if @snippet.errors.any? -if @snippet.errors.any?
.alert.alert-error .alert.alert-error
%ul %ul
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
= f.submit 'Save', class: "btn-save btn" = f.submit 'Save', class: "btn-save btn"
= link_to "Cancel", project_snippets_path(@project), class: " btn" = link_to "Cancel", project_snippets_path(@project), class: " btn"
- unless @snippet.new_record? - unless @snippet.new_record?
.pull-right= link_to 'Destroy', [@project, @snippet], confirm: 'Are you sure?', method: :delete, class: "btn pull-right danger delete-snippet", id: "destroy_snippet_#{@snippet.id}" .pull-right= link_to 'Destroy', project_snippet_path(@project, @snippet), confirm: 'Are you sure?', method: :delete, class: "btn pull-right danger delete-snippet", id: "destroy_snippet_#{@snippet.id}"
:javascript :javascript
......
= render "snippets/form" = render "projects/snippets/form", url: project_snippet_path(@project, @snippet)
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
%th Title %th Title
%th File Name %th File Name
%th Expires At %th Expires At
= render @snippets = render partial: "projects/snippets/snippet", collection: @snippets
- if @snippets.empty? - if @snippets.empty?
%tr %tr
%td{colspan: 3} %td{colspan: 3}
......
= render "snippets/form" = render "projects/snippets/form", url: project_snippets_path(@project, @snippet)
...@@ -5,5 +5,5 @@ ...@@ -5,5 +5,5 @@
= link_to "Edit", edit_project_snippet_path(@project, @snippet), class: "btn btn-small pull-right", title: 'Edit Snippet' = link_to "Edit", edit_project_snippet_path(@project, @snippet), class: "btn btn-small pull-right", title: 'Edit Snippet'
%br %br
%div= render 'blob' %div= render 'projects/snippets/blob'
%div#notes= render "notes/notes_with_form" %div#notes= render "notes/notes_with_form"
...@@ -177,6 +177,14 @@ Gitlab::Application.routes.draw do ...@@ -177,6 +177,14 @@ Gitlab::Application.routes.draw do
match "/compare/:from...:to" => "compare#show", as: "compare", match "/compare/:from...:to" => "compare#show", as: "compare",
:via => [:get, :post], constraints: {from: /.+/, to: /.+/} :via => [:get, :post], constraints: {from: /.+/, to: /.+/}
scope module: :projects do
resources :snippets do
member do
get "raw"
end
end
end
resources :wikis, only: [:show, :edit, :destroy, :create] do resources :wikis, only: [:show, :edit, :destroy, :create] do
collection do collection do
get :pages get :pages
...@@ -244,21 +252,12 @@ Gitlab::Application.routes.draw do ...@@ -244,21 +252,12 @@ Gitlab::Application.routes.draw do
end end
end end
scope module: :projects do
resources :snippets do
member do
get "raw"
end
end
end
resources :hooks, only: [:index, :create, :destroy] do resources :hooks, only: [:index, :create, :destroy] do
member do member do
get :test get :test
end end
end end
resources :team, controller: 'team_members', only: [:index] resources :team, controller: 'team_members', only: [:index]
resources :milestones, except: [:destroy] resources :milestones, except: [:destroy]
resources :labels, only: [:index] resources :labels, only: [:index]
......
class ChangeProjectIdToNullInSnipepts < ActiveRecord::Migration
def up
change_column :snippets, :project_id, :integer, :null => true
end
def down
change_column :snippets, :project_id, :integer, :null => false
end
end
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
# #
# It's strongly recommended to check this file into your version control system. # It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20130324151736) do ActiveRecord::Schema.define(:version => 20130324172327) do
create_table "events", :force => true do |t| create_table "events", :force => true do |t|
t.string "target_type" t.string "target_type"
...@@ -185,7 +185,7 @@ ActiveRecord::Schema.define(:version => 20130324151736) do ...@@ -185,7 +185,7 @@ ActiveRecord::Schema.define(:version => 20130324151736) do
t.string "title" t.string "title"
t.text "content" t.text "content"
t.integer "author_id", :null => false t.integer "author_id", :null => false
t.integer "project_id", :null => false t.integer "project_id"
t.datetime "created_at", :null => false t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false t.datetime "updated_at", :null => false
t.string "file_name" t.string "file_name"
......
Feature: Project Snippets
Background:
Given I sign in as a user
And I own project "Shop"
And project "Shop" have "Snippet one" snippet
And project "Shop" have no "Snippet two" snippet
And I visit project "Shop" snippets page
Scenario: I should see snippets
Given I visit project "Shop" snippets page
Then I should see "Snippet one" in snippets
And I should not see "Snippet two" in snippets
Scenario: I create new project snippet
Given I click link "New Snippet"
And I submit new snippet "Snippet three"
Then I should see snippet "Snippet three"
@javascript
Scenario: I comment on a snippet "Snippet one"
Given I visit snippet page "Snippet one"
And I leave a comment like "Good snippet!"
Then I should see comment "Good snippet!"
Scenario: I update "Snippet one"
Given I visit snippet page "Snippet one"
And I click link "Edit"
And I submit new title "Snippet new title"
Then I should see "Snippet new title"
Scenario: I destroy "Snippet one"
Given I visit snippet page "Snippet one"
And I click link "Edit"
And I click link "Destroy"
Then I should not see "Snippet one" in snippets
class ProjectSnippets < Spinach::FeatureSteps
include SharedAuthentication
include SharedProject
include SharedNote
include SharedPaths
And 'project "Shop" have "Snippet one" snippet' do
create(:project_snippet,
title: "Snippet one",
content: "Test content",
file_name: "snippet.rb",
project: project,
author: project.users.first)
end
And 'project "Shop" have no "Snippet two" snippet' do
create(:snippet,
title: "Snippet two",
content: "Test content",
file_name: "snippet.rb",
author: project.users.first)
end
Given 'I click link "New Snippet"' do
click_link "Add new snippet"
end
Given 'I click link "Snippet one"' do
click_link "Snippet one"
end
Then 'I should see "Snippet one" in snippets' do
page.should have_content "Snippet one"
end
And 'I should not see "Snippet two" in snippets' do
page.should_not have_content "Snippet two"
end
And 'I should not see "Snippet one" in snippets' do
page.should_not have_content "Snippet one"
end
And 'I click link "Edit"' do
within ".page_title" do
click_link "Edit"
end
end
And 'I click link "Destroy"' do
click_link "Destroy"
end
And 'I submit new snippet "Snippet three"' do
fill_in "project_snippet_title", :with => "Snippet three"
select "forever", :from => "project_snippet_expires_at"
fill_in "project_snippet_file_name", :with => "my_snippet.rb"
within('.file-editor') do
find(:xpath, "//input[@id='project_snippet_content']").set 'Content of snippet three'
end
click_button "Save"
end
Then 'I should see snippet "Snippet three"' do
page.should have_content "Snippet three"
page.should have_content "Content of snippet three"
end
And 'I submit new title "Snippet new title"' do
fill_in "project_snippet_title", :with => "Snippet new title"
click_button "Save"
end
Then 'I should see "Snippet new title"' do
page.should have_content "Snippet new title"
end
And 'I leave a comment like "Good snippet!"' do
within('.js-main-target-form') do
fill_in "note_note", with: "Good snippet!"
click_button "Add Comment"
end
end
Then 'I should see comment "Good snippet!"' do
page.should have_content "Good snippet!"
end
And 'I visit snippet page "Snippet one"' do
visit project_snippet_path(project, project_snippet)
end
def project
@project ||= Project.find_by_name!("Shop")
end
def project_snippet
@project_snippet ||= ProjectSnippet.find_by_title!("Snippet One")
end
end
...@@ -263,6 +263,10 @@ module SharedPaths ...@@ -263,6 +263,10 @@ module SharedPaths
visit project_wiki_path(@project, :home) visit project_wiki_path(@project, :home)
end end
Given 'I visit project "Shop" snippets page' do
visit project_snippets_path(project)
end
def root_ref def root_ref
@project.repository.root_ref @project.repository.root_ref
end end
......
...@@ -196,7 +196,7 @@ FactoryGirl.define do ...@@ -196,7 +196,7 @@ FactoryGirl.define do
user user
end end
factory :snippet do factory :project_snippet do
project project
author author
title title
...@@ -204,6 +204,13 @@ FactoryGirl.define do ...@@ -204,6 +204,13 @@ FactoryGirl.define do
file_name file_name
end end
factory :snippet do
author
title
content
file_name
end
factory :protected_branch do factory :protected_branch do
name name
project project
......
require 'spec_helper'
describe "Project::Snippets" do
let(:project) { create(:project) }
before do
login_as :user
project.team << [@user, :developer]
end
describe "GET /:project/snippets" do
before do
@snippet = create(:snippet,
author: @user,
project: project)
visit project_snippets_path(project)
p project_snippets_path(project)
end
subject { page }
it { should have_content(@snippet.title[0..10]) }
it { should have_content(@snippet.project.name) }
describe "Destroy" do
before do
# admin access to remove snippet
@user.users_projects.destroy_all
project.team << [@user, :master]
visit edit_project_snippet_path(project, @snippet)
end
it "should remove entry" do
expect {
click_link "destroy_snippet_#{@snippet.id}"
}.to change { Snippet.count }.by(-1)
end
end
end
describe "New project snippet" do
before do
visit project_snippets_path(project)
click_link "New Snippet"
end
it "should open new snippet popup" do
page.current_path.should == new_project_snippet_path(project)
end
describe "fill in", js: true do
before do
fill_in "snippet_title", with: "login function"
fill_in "snippet_file_name", with: "test.rb"
page.execute_script("editor.insert('def login; end');")
end
it { expect { click_button "Save" }.to change {Snippet.count}.by(1) }
it "should add new snippet to table" do
click_button "Save"
page.current_path.should == project_snippet_path(project, Snippet.last)
page.should have_content "login function"
page.should have_content "test.rb"
end
end
end
describe "Edit project snippet" do
before do
@snippet = create(:snippet,
author: @user,
project: project)
visit project_snippet_path(project, @snippet)
click_link "Edit Snippet"
end
it "should open edit page" do
page.current_path.should == edit_project_snippet_path(project, @snippet)
end
describe "fill in" do
before do
fill_in "snippet_title", with: "login function"
fill_in "snippet_file_name", with: "test.rb"
end
it { expect { click_button "Save" }.to_not change {Snippet.count} }
it "should update snippet fields" do
click_button "Save"
page.current_path.should == project_snippet_path(project, @snippet)
page.should have_content "login function"
page.should have_content "test.rb"
end
end
end
end
require 'spec_helper'
describe "Snippets" do
let(:project) { create(:project) }
before do
login_as :user
project.team << [@user, :developer]
end
describe "GET /snippets" do
before do
@snippet = create(:snippet,
author: @user,
project: project)
visit project_snippets_path(project)
end
subject { page }
it { should have_content(@snippet.title[0..10]) }
it { should have_content(@snippet.project.name) }
describe "Destroy" do
before do
# admin access to remove snippet
@user.users_projects.destroy_all
project.team << [@user, :master]
visit edit_project_snippet_path(project, @snippet)
end
it "should remove entry" do
expect {
click_link "destroy_snippet_#{@snippet.id}"
}.to change { Snippet.count }.by(-1)
end
end
end
describe "New snippet" do
before do
visit project_snippets_path(project)
click_link "New Snippet"
end
it "should open new snippet popup" do
page.current_path.should == new_project_snippet_path(project)
end
describe "fill in", js: true do
before do
fill_in "snippet_title", with: "login function"
fill_in "snippet_file_name", with: "test.rb"
page.execute_script("editor.insert('def login; end');")
end
it { expect { click_button "Save" }.to change {Snippet.count}.by(1) }
it "should add new snippet to table" do
click_button "Save"
page.current_path.should == project_snippet_path(project, Snippet.last)
page.should have_content "login function"
page.should have_content "test.rb"
end
end
end
describe "Edit snippet" do
before do
@snippet = create(:snippet,
author: @user,
project: project)
visit project_snippet_path(project, @snippet)
click_link "Edit Snippet"
end
it "should open edit page" do
page.current_path.should == edit_project_snippet_path(project, @snippet)
end
describe "fill in" do
before do
fill_in "snippet_title", with: "login function"
fill_in "snippet_file_name", with: "test.rb"
end
it { expect { click_button "Save" }.to_not change {Snippet.count} }
it "should update snippet fields" do
click_button "Save"
page.current_path.should == project_snippet_path(project, @snippet)
page.should have_content "login function"
page.should have_content "test.rb"
end
end
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