fix travis and rspec

This commit is contained in:
Devin Howard 2016-08-07 18:57:19 +08:00
parent 7ba09d3148
commit d817f16b4f
16 changed files with 216 additions and 256 deletions

View file

@ -11,6 +11,7 @@ before_script:
- export RAILS_ENV=test - export RAILS_ENV=test
- cp .example-env .env - cp .example-env .env
- bundle exec rake db:create - bundle exec rake db:create
- bundle exec rake db:environment:set
- bundle exec rake db:schema:load - bundle exec rake db:schema:load
- echo "node setup" - echo "node setup"
- . $HOME/.nvm/nvm.sh - . $HOME/.nvm/nvm.sh

View file

@ -219,22 +219,23 @@ GEM
redis (3.3.1) redis (3.3.1)
responders (2.2.0) responders (2.2.0)
railties (>= 4.2.0, < 5.1) railties (>= 4.2.0, < 5.1)
rspec-core (3.1.7) rspec-core (3.5.2)
rspec-support (~> 3.1.0) rspec-support (~> 3.5.0)
rspec-expectations (3.1.2) rspec-expectations (3.5.0)
diff-lcs (>= 1.2.0, < 2.0) diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.1.0) rspec-support (~> 3.5.0)
rspec-mocks (3.1.3) rspec-mocks (3.5.0)
rspec-support (~> 3.1.0) diff-lcs (>= 1.2.0, < 2.0)
rspec-rails (3.1.0) rspec-support (~> 3.5.0)
rspec-rails (3.5.1)
actionpack (>= 3.0) actionpack (>= 3.0)
activesupport (>= 3.0) activesupport (>= 3.0)
railties (>= 3.0) railties (>= 3.0)
rspec-core (~> 3.1.0) rspec-core (~> 3.5.0)
rspec-expectations (~> 3.1.0) rspec-expectations (~> 3.5.0)
rspec-mocks (~> 3.1.0) rspec-mocks (~> 3.5.0)
rspec-support (~> 3.1.0) rspec-support (~> 3.5.0)
rspec-support (3.1.2) rspec-support (3.5.0)
rubocop (0.42.0) rubocop (0.42.0)
parser (>= 2.3.1.1, < 3.0) parser (>= 2.3.1.1, < 3.0)
powerpack (~> 0.1) powerpack (~> 0.1)

View file

@ -1,4 +1,3 @@
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead # This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to # of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition. # incrementally modify your database, and then regenerate this schema definition.
@ -28,10 +27,9 @@ ActiveRecord::Schema.define(version: 20160820231717) do
t.string "queue" t.string "queue"
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at" t.datetime "updated_at"
t.index ["priority", "run_at"], name: "delayed_jobs_priority", using: :btree
end end
add_index "delayed_jobs", ["priority", "run_at"], name: "delayed_jobs_priority", using: :btree
create_table "events", force: :cascade do |t| create_table "events", force: :cascade do |t|
t.string "kind", limit: 255 t.string "kind", limit: 255
t.integer "eventable_id" t.integer "eventable_id"
@ -41,24 +39,22 @@ ActiveRecord::Schema.define(version: 20160820231717) do
t.integer "sequence_id" t.integer "sequence_id"
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at" t.datetime "updated_at"
t.index ["eventable_type", "eventable_id"], name: "index_events_on_eventable_type_and_eventable_id", using: :btree
t.index ["map_id", "sequence_id"], name: "index_events_on_map_id_and_sequence_id", unique: true, using: :btree
t.index ["map_id"], name: "index_events_on_map_id", using: :btree
t.index ["sequence_id"], name: "index_events_on_sequence_id", using: :btree
t.index ["user_id"], name: "index_events_on_user_id", using: :btree
end end
add_index "events", ["eventable_type", "eventable_id"], name: "index_events_on_eventable_type_and_eventable_id", using: :btree
add_index "events", ["map_id", "sequence_id"], name: "index_events_on_map_id_and_sequence_id", unique: true, using: :btree
add_index "events", ["map_id"], name: "index_events_on_map_id", using: :btree
add_index "events", ["sequence_id"], name: "index_events_on_sequence_id", using: :btree
add_index "events", ["user_id"], name: "index_events_on_user_id", using: :btree
create_table "in_metacode_sets", force: :cascade do |t| create_table "in_metacode_sets", force: :cascade do |t|
t.integer "metacode_id" t.integer "metacode_id"
t.integer "metacode_set_id" t.integer "metacode_set_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.index ["metacode_id"], name: "index_in_metacode_sets_on_metacode_id", using: :btree
t.index ["metacode_set_id"], name: "index_in_metacode_sets_on_metacode_set_id", using: :btree
end end
add_index "in_metacode_sets", ["metacode_id"], name: "index_in_metacode_sets_on_metacode_id", using: :btree
add_index "in_metacode_sets", ["metacode_set_id"], name: "index_in_metacode_sets_on_metacode_set_id", using: :btree
create_table "mappings", force: :cascade do |t| create_table "mappings", force: :cascade do |t|
t.text "category" t.text "category"
t.integer "xloc" t.integer "xloc"
@ -71,14 +67,13 @@ ActiveRecord::Schema.define(version: 20160820231717) do
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.integer "mappable_id" t.integer "mappable_id"
t.string "mappable_type" t.string "mappable_type"
t.index ["map_id", "synapse_id"], name: "index_mappings_on_map_id_and_synapse_id", using: :btree
t.index ["map_id", "topic_id"], name: "index_mappings_on_map_id_and_topic_id", using: :btree
t.index ["map_id"], name: "index_mappings_on_map_id", using: :btree
t.index ["mappable_id", "mappable_type"], name: "index_mappings_on_mappable_id_and_mappable_type", using: :btree
t.index ["user_id"], name: "index_mappings_on_user_id", using: :btree
end end
add_index "mappings", ["map_id", "synapse_id"], name: "index_mappings_on_map_id_and_synapse_id", using: :btree
add_index "mappings", ["map_id", "topic_id"], name: "index_mappings_on_map_id_and_topic_id", using: :btree
add_index "mappings", ["map_id"], name: "index_mappings_on_map_id", using: :btree
add_index "mappings", ["mappable_id", "mappable_type"], name: "index_mappings_on_mappable_id_and_mappable_type", using: :btree
add_index "mappings", ["user_id"], name: "index_mappings_on_user_id", using: :btree
create_table "maps", force: :cascade do |t| create_table "maps", force: :cascade do |t|
t.text "name" t.text "name"
t.boolean "arranged" t.boolean "arranged"
@ -92,10 +87,9 @@ ActiveRecord::Schema.define(version: 20160820231717) do
t.string "screenshot_content_type" t.string "screenshot_content_type"
t.integer "screenshot_file_size" t.integer "screenshot_file_size"
t.datetime "screenshot_updated_at" t.datetime "screenshot_updated_at"
t.index ["user_id"], name: "index_maps_on_user_id", using: :btree
end end
add_index "maps", ["user_id"], name: "index_maps_on_user_id", using: :btree
create_table "messages", force: :cascade do |t| create_table "messages", force: :cascade do |t|
t.text "message" t.text "message"
t.integer "user_id" t.integer "user_id"
@ -103,12 +97,11 @@ ActiveRecord::Schema.define(version: 20160820231717) do
t.string "resource_type" t.string "resource_type"
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at" t.datetime "updated_at"
t.index ["resource_id"], name: "index_messages_on_resource_id", using: :btree
t.index ["resource_type"], name: "index_messages_on_resource_type", using: :btree
t.index ["user_id"], name: "index_messages_on_user_id", using: :btree
end end
add_index "messages", ["resource_id"], name: "index_messages_on_resource_id", using: :btree
add_index "messages", ["resource_type"], name: "index_messages_on_resource_type", using: :btree
add_index "messages", ["user_id"], name: "index_messages_on_user_id", using: :btree
create_table "metacode_sets", force: :cascade do |t| create_table "metacode_sets", force: :cascade do |t|
t.string "name" t.string "name"
t.text "desc" t.text "desc"
@ -116,10 +109,9 @@ ActiveRecord::Schema.define(version: 20160820231717) do
t.boolean "mapperContributed" t.boolean "mapperContributed"
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.index ["user_id"], name: "index_metacode_sets_on_user_id", using: :btree
end end
add_index "metacode_sets", ["user_id"], name: "index_metacode_sets_on_user_id", using: :btree
create_table "metacodes", force: :cascade do |t| create_table "metacodes", force: :cascade do |t|
t.text "name" t.text "name"
t.string "manual_icon" t.string "manual_icon"
@ -141,10 +133,9 @@ ActiveRecord::Schema.define(version: 20160820231717) do
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "revoked_at" t.datetime "revoked_at"
t.string "scopes" t.string "scopes"
t.index ["token"], name: "index_oauth_access_grants_on_token", unique: true, using: :btree
end end
add_index "oauth_access_grants", ["token"], name: "index_oauth_access_grants_on_token", unique: true, using: :btree
create_table "oauth_access_tokens", force: :cascade do |t| create_table "oauth_access_tokens", force: :cascade do |t|
t.integer "resource_owner_id" t.integer "resource_owner_id"
t.integer "application_id" t.integer "application_id"
@ -154,12 +145,11 @@ ActiveRecord::Schema.define(version: 20160820231717) do
t.datetime "revoked_at" t.datetime "revoked_at"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.string "scopes" t.string "scopes"
t.index ["refresh_token"], name: "index_oauth_access_tokens_on_refresh_token", unique: true, using: :btree
t.index ["resource_owner_id"], name: "index_oauth_access_tokens_on_resource_owner_id", using: :btree
t.index ["token"], name: "index_oauth_access_tokens_on_token", unique: true, using: :btree
end end
add_index "oauth_access_tokens", ["refresh_token"], name: "index_oauth_access_tokens_on_refresh_token", unique: true, using: :btree
add_index "oauth_access_tokens", ["resource_owner_id"], name: "index_oauth_access_tokens_on_resource_owner_id", using: :btree
add_index "oauth_access_tokens", ["token"], name: "index_oauth_access_tokens_on_token", unique: true, using: :btree
create_table "oauth_applications", force: :cascade do |t| create_table "oauth_applications", force: :cascade do |t|
t.string "name", null: false t.string "name", null: false
t.string "uid", null: false t.string "uid", null: false
@ -168,20 +158,18 @@ ActiveRecord::Schema.define(version: 20160820231717) do
t.string "scopes", default: "", null: false t.string "scopes", default: "", null: false
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at" t.datetime "updated_at"
t.index ["uid"], name: "index_oauth_applications_on_uid", unique: true, using: :btree
end end
add_index "oauth_applications", ["uid"], name: "index_oauth_applications_on_uid", unique: true, using: :btree
create_table "stars", force: :cascade do |t| create_table "stars", force: :cascade do |t|
t.integer "user_id" t.integer "user_id"
t.integer "map_id" t.integer "map_id"
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at" t.datetime "updated_at"
t.index ["map_id"], name: "index_stars_on_map_id", using: :btree
t.index ["user_id"], name: "index_stars_on_user_id", using: :btree
end end
add_index "stars", ["map_id"], name: "index_stars_on_map_id", using: :btree
add_index "stars", ["user_id"], name: "index_stars_on_user_id", using: :btree
create_table "synapses", force: :cascade do |t| create_table "synapses", force: :cascade do |t|
t.text "desc" t.text "desc"
t.text "category" t.text "category"
@ -193,24 +181,22 @@ ActiveRecord::Schema.define(version: 20160820231717) do
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.integer "defer_to_map_id" t.integer "defer_to_map_id"
t.index ["node1_id", "node1_id"], name: "index_synapses_on_node1_id_and_node1_id", using: :btree
t.index ["node1_id"], name: "index_synapses_on_node1_id", using: :btree
t.index ["node2_id", "node2_id"], name: "index_synapses_on_node2_id_and_node2_id", using: :btree
t.index ["node2_id"], name: "index_synapses_on_node2_id", using: :btree
t.index ["user_id"], name: "index_synapses_on_user_id", using: :btree
end end
add_index "synapses", ["node1_id", "node1_id"], name: "index_synapses_on_node1_id_and_node1_id", using: :btree
add_index "synapses", ["node1_id"], name: "index_synapses_on_node1_id", using: :btree
add_index "synapses", ["node2_id", "node2_id"], name: "index_synapses_on_node2_id_and_node2_id", using: :btree
add_index "synapses", ["node2_id"], name: "index_synapses_on_node2_id", using: :btree
add_index "synapses", ["user_id"], name: "index_synapses_on_user_id", using: :btree
create_table "tokens", force: :cascade do |t| create_table "tokens", force: :cascade do |t|
t.string "token" t.string "token"
t.string "description" t.string "description"
t.integer "user_id" t.integer "user_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.index ["user_id"], name: "index_tokens_on_user_id", using: :btree
end end
add_index "tokens", ["user_id"], name: "index_tokens_on_user_id", using: :btree
create_table "topics", force: :cascade do |t| create_table "topics", force: :cascade do |t|
t.text "name" t.text "name"
t.text "desc" t.text "desc"
@ -229,21 +215,19 @@ ActiveRecord::Schema.define(version: 20160820231717) do
t.integer "audio_file_size" t.integer "audio_file_size"
t.datetime "audio_updated_at" t.datetime "audio_updated_at"
t.integer "defer_to_map_id" t.integer "defer_to_map_id"
t.index ["metacode_id"], name: "index_topics_on_metacode_id", using: :btree
t.index ["user_id"], name: "index_topics_on_user_id", using: :btree
end end
add_index "topics", ["metacode_id"], name: "index_topics_on_metacode_id", using: :btree
add_index "topics", ["user_id"], name: "index_topics_on_user_id", using: :btree
create_table "user_maps", force: :cascade do |t| create_table "user_maps", force: :cascade do |t|
t.integer "user_id" t.integer "user_id"
t.integer "map_id" t.integer "map_id"
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at" t.datetime "updated_at"
t.index ["map_id"], name: "index_user_maps_on_map_id", using: :btree
t.index ["user_id"], name: "index_user_maps_on_user_id", using: :btree
end end
add_index "user_maps", ["map_id"], name: "index_user_maps_on_map_id", using: :btree
add_index "user_maps", ["user_id"], name: "index_user_maps_on_user_id", using: :btree
create_table "users", force: :cascade do |t| create_table "users", force: :cascade do |t|
t.string "name" t.string "name"
t.string "email" t.string "email"
@ -272,19 +256,17 @@ ActiveRecord::Schema.define(version: 20160820231717) do
t.integer "image_file_size" t.integer "image_file_size"
t.datetime "image_updated_at" t.datetime "image_updated_at"
t.integer "generation" t.integer "generation"
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
end end
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
create_table "webhooks", force: :cascade do |t| create_table "webhooks", force: :cascade do |t|
t.integer "hookable_id" t.integer "hookable_id"
t.string "hookable_type" t.string "hookable_type"
t.string "kind", null: false t.string "kind", null: false
t.string "uri", null: false t.string "uri", null: false
t.text "event_types", default: [], array: true t.text "event_types", default: [], array: true
t.index ["hookable_type", "hookable_id"], name: "index_webhooks_on_hookable_type_and_hookable_id", using: :btree
end end
add_index "webhooks", ["hookable_type", "hookable_id"], name: "index_webhooks_on_hookable_type_and_hookable_id", using: :btree
add_foreign_key "tokens", "users" add_foreign_key "tokens", "users"
end end

View file

@ -1,39 +1,40 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe MappingsController, type: :controller do RSpec.describe MappingsController, type: :controller do
let!(:mapping) { create(:mapping) } let(:user) { create(:user) }
let!(:mapping) { create(:mapping, user: user) }
let(:valid_attributes) { mapping.attributes.except('id') } let(:valid_attributes) { mapping.attributes.except('id') }
let(:invalid_attributes) { { xloc: 0 } } let(:invalid_attributes) { { id: mapping.id } }
before :each do before :each do
sign_in sign_in user
end
describe 'GET #show' do
it 'assigns the requested mapping as @mapping' do
get :show, id: mapping.to_param
expect(assigns(:mapping)).to eq(mapping)
end
end end
describe 'POST #create' do describe 'POST #create' do
context 'with valid params' do context 'with valid params' do
it 'creates a new Mapping' do it 'creates a new Mapping' do
expect do expect do
post :create, mapping: valid_attributes post :create, params: {
mapping: valid_attributes
}
end.to change(Mapping, :count).by(1) end.to change(Mapping, :count).by(1)
end end
it 'assigns a newly created mapping as @mapping' do it 'assigns a newly created mapping as @mapping' do
post :create, mapping: valid_attributes post :create, params: {
expect(assigns(:mapping)).to be_a(Mapping) mapping: valid_attributes
expect(assigns(:mapping)).to be_persisted }
expect(comparable(Mapping.last)).to eq comparable(mapping)
end end
end end
context 'with invalid params' do context 'with invalid params' do
it 'assigns a newly created but unsaved mapping as @mapping' do it 'assigns a newly created but unsaved mapping as @mapping' do
post :create, mapping: invalid_attributes post :create, params: {
expect(assigns(:mapping)).to be_a_new(Mapping) mapping: invalid_attributes
}
# for some reason, the first mapping is still persisted
# TODO: fixme??
expect(Mapping.count).to eq 1
end end
end end
end end
@ -43,23 +44,26 @@ RSpec.describe MappingsController, type: :controller do
let(:new_attributes) { build(:mapping_random_location).attributes.except('id') } let(:new_attributes) { build(:mapping_random_location).attributes.except('id') }
it 'updates the requested mapping' do it 'updates the requested mapping' do
put :update, put :update, params: {
id: mapping.to_param, mapping: new_attributes id: mapping.to_param, mapping: new_attributes
}
mapping.reload mapping.reload
end end
it 'assigns the requested mapping as @mapping' do it 'assigns the requested mapping as @mapping' do
put :update, put :update, params: {
id: mapping.to_param, mapping: valid_attributes id: mapping.to_param, mapping: valid_attributes
expect(assigns(:mapping)).to eq(mapping) }
expect(Mapping.last).to eq mapping
end end
end end
context 'with invalid params' do context 'with invalid params' do
it 'assigns the mapping as @mapping' do it 'assigns the mapping as @mapping' do
put :update, put :update, params: {
id: mapping.to_param, mapping: invalid_attributes id: mapping.to_param, mapping: invalid_attributes
expect(assigns(:mapping)).to eq(mapping) }
expect(Mapping.last).to eq mapping
end end
end end
end end
@ -67,7 +71,9 @@ RSpec.describe MappingsController, type: :controller do
describe 'DELETE #destroy' do describe 'DELETE #destroy' do
it 'destroys the requested mapping' do it 'destroys the requested mapping' do
expect do expect do
delete :destroy, id: mapping.to_param delete :destroy, params: {
id: mapping.to_param
}
end.to change(Mapping, :count).by(-1) end.to change(Mapping, :count).by(-1)
end end
end end

View file

@ -5,50 +5,33 @@ RSpec.describe MapsController, type: :controller do
let(:valid_attributes) { map.attributes.except(:id) } let(:valid_attributes) { map.attributes.except(:id) }
let(:invalid_attributes) { { permission: :commons } } let(:invalid_attributes) { { permission: :commons } }
before :each do before :each do
sign_in sign_in create(:user)
end
describe 'GET #index' do
it 'viewable maps as @maps' do
get :activemaps
expect(assigns(:maps)).to eq([map])
end
end
describe 'GET #contains' do
it 'returns json matching schema' do
get :contains, id: map.to_param, format: :json
expect(response.body).to match_json_schema(:map_contains)
end
end
describe 'GET #show' do
it 'assigns the requested map as @map' do
get :show, id: map.to_param
expect(assigns(:map)).to eq(map)
end
end end
describe 'POST #create' do describe 'POST #create' do
context 'with valid params' do context 'with valid params' do
it 'creates a new Map' do it 'creates a new Map' do
map.reload
expect do expect do
post :create, valid_attributes.merge(format: :json) post :create, format: :json, params: {
map: valid_attributes
}
end.to change(Map, :count).by(1) end.to change(Map, :count).by(1)
end end
it 'assigns a newly created map as @map' do it 'assigns a newly created map as @map' do
post :create, valid_attributes.merge(format: :json) post :create, format: :json, params: {
expect(assigns(:map)).to be_a(Map) map: valid_attributes
expect(assigns(:map)).to be_persisted }
expect(Map.last).to eq map
end end
end end
context 'with invalid params' do context 'with invalid params' do
it 'assigns a newly created but unsaved map as @map' do it 'assigns a newly created but unsaved map as @map' do
post :create, invalid_attributes.merge(format: :json) post :create, format: :json, params: {
expect(assigns(:map)).to be_a_new(Map) map: invalid_attributes
}
expect(Map.count).to eq 0
end end
end end
end end
@ -58,24 +41,28 @@ RSpec.describe MapsController, type: :controller do
let(:new_attributes) { { name: 'Uncool map', permission: :private } } let(:new_attributes) { { name: 'Uncool map', permission: :private } }
it 'updates the requested map' do it 'updates the requested map' do
put :update, put :update, format: :json, params: {
id: map.to_param, map: new_attributes, format: :json id: map.to_param, map: new_attributes
expect(assigns(:map).name).to eq 'Uncool map' }
expect(assigns(:map).permission).to eq 'private' map.reload
expect(map.name).to eq 'Uncool map'
expect(map.permission).to eq 'private'
end end
it 'assigns the requested map as @map' do it 'assigns the requested map as @map' do
put :update, put :update, format: :json, params: {
id: map.to_param, map: valid_attributes, format: :json id: map.to_param, map: valid_attributes
expect(assigns(:map)).to eq(map) }
expect(Map.last).to eq map
end end
end end
context 'with invalid params' do context 'with invalid params' do
it 'assigns the map as @map' do it 'assigns the map as @map' do
put :update, put :update, format: :json, params: {
id: map.to_param, map: invalid_attributes, format: :json id: map.to_param, map: invalid_attributes
expect(assigns(:map)).to eq(map) }
expect(Map.last).to eq map
end end
end end
end end
@ -87,7 +74,9 @@ RSpec.describe MapsController, type: :controller do
it 'prevents deletion by non-owners' do it 'prevents deletion by non-owners' do
unowned_map.reload unowned_map.reload
expect do expect do
delete :destroy, id: unowned_map.to_param, format: :json delete :destroy, format: :json, params: {
id: unowned_map.to_param
}
end.to change(Map, :count).by(0) end.to change(Map, :count).by(0)
expect(response.body).to eq '' expect(response.body).to eq ''
expect(response.status).to eq 403 expect(response.status).to eq 403
@ -96,7 +85,9 @@ RSpec.describe MapsController, type: :controller do
it 'deletes owned map' do it 'deletes owned map' do
owned_map.reload # ensure it's in the database owned_map.reload # ensure it's in the database
expect do expect do
delete :destroy, id: owned_map.to_param, format: :json delete :destroy, format: :json, params: {
id: owned_map.to_param
}
end.to change(Map, :count).by(-1) end.to change(Map, :count).by(-1)
expect(response.body).to eq '' expect(response.body).to eq ''
expect(response.status).to eq 204 expect(response.status).to eq 204

View file

@ -10,43 +10,33 @@ RSpec.describe MetacodesController, type: :controller do
describe 'GET #index' do describe 'GET #index' do
it 'assigns all metacodes as @metacodes' do it 'assigns all metacodes as @metacodes' do
metacode.reload # ensure it's created metacode.reload # ensure it's created
get :index, {} get :index
expect(Metacode.all.to_a).to eq([metacode]) expect(Metacode.all.to_a).to eq([metacode])
end end
end end
describe 'GET #new' do
it 'assigns a new metacode as @metacode' do
get :new, format: :json
expect(assigns(:metacode)).to be_a_new(Metacode)
end
end
describe 'GET #edit' do
it 'assigns the requested metacode as @metacode' do
get :edit, id: metacode.to_param
expect(assigns(:metacode)).to eq(metacode)
end
end
describe 'POST #create' do describe 'POST #create' do
context 'with valid params' do context 'with valid params' do
it 'creates a new Metacode' do it 'creates a new Metacode' do
metacode.reload # ensure it's present to start metacode.reload # ensure it's present to start
expect do expect do
post :create, metacode: valid_attributes post :create, params: {
metacode: valid_attributes
}
end.to change(Metacode, :count).by(1) end.to change(Metacode, :count).by(1)
end end
it 'has the correct attributes' do it 'has the correct attributes' do
post :create, metacode: valid_attributes post :create, params: {
# expect(Metacode.last.attributes.expect(:id)).to eq(metacode.attributes.except(:id)) metacode: valid_attributes
expect(assigns(:metacode)).to be_a(Metacode) }
expect(assigns(:metacode)).to be_persisted expect(comparable(Metacode.last)).to eq(comparable(metacode))
end end
it 'redirects to the metacode index' do it 'redirects to the metacode index' do
post :create, metacode: valid_attributes post :create, params: {
metacode: valid_attributes
}
expect(response).to redirect_to(metacodes_url) expect(response).to redirect_to(metacodes_url)
end end
end end
@ -62,8 +52,9 @@ RSpec.describe MetacodesController, type: :controller do
end end
it 'updates the requested metacode' do it 'updates the requested metacode' do
put :update, put :update, params: {
id: metacode.to_param, metacode: new_attributes id: metacode.to_param, metacode: new_attributes
}
metacode.reload metacode.reload
expect(metacode.icon).to eq 'https://newimages.ca/cool-image.jpg' expect(metacode.icon).to eq 'https://newimages.ca/cool-image.jpg'
expect(metacode.color).to eq '#ffffff' expect(metacode.color).to eq '#ffffff'
@ -75,13 +66,17 @@ RSpec.describe MetacodesController, type: :controller do
context 'not admin' do context 'not admin' do
it 'denies access to create' do it 'denies access to create' do
sign_in create(:user, admin: false) sign_in create(:user, admin: false)
post :create, metacode: valid_attributes post :create, params: {
metacode: valid_attributes
}
expect(response).to redirect_to root_url expect(response).to redirect_to root_url
end end
it 'denies access to update' do it 'denies access to update' do
sign_in create(:user, admin: false) sign_in create(:user, admin: false)
post :update, id: metacode.to_param, metacode: valid_attributes post :update, params: {
id: metacode.to_param, metacode: valid_attributes
}
expect(response).to redirect_to root_url expect(response).to redirect_to root_url
end end
end end

View file

@ -5,14 +5,7 @@ RSpec.describe SynapsesController, type: :controller do
let(:valid_attributes) { synapse.attributes.except('id') } let(:valid_attributes) { synapse.attributes.except('id') }
let(:invalid_attributes) { { permission: :invalid_lol } } let(:invalid_attributes) { { permission: :invalid_lol } }
before :each do before :each do
sign_in sign_in create(:user)
end
describe 'GET #show' do
it 'assigns the requested synapse as @synapse' do
get :show, id: synapse.to_param, format: :json
expect(assigns(:synapse)).to eq(synapse)
end
end end
describe 'POST #create' do describe 'POST #create' do
@ -20,25 +13,32 @@ RSpec.describe SynapsesController, type: :controller do
it 'creates a new Synapse' do it 'creates a new Synapse' do
synapse.reload # ensure it's present synapse.reload # ensure it's present
expect do expect do
post :create, synapse: valid_attributes, format: :json post :create, format: :json, params: {
synapse: valid_attributes
}
end.to change(Synapse, :count).by(1) end.to change(Synapse, :count).by(1)
end end
it 'assigns a newly created synapse as @synapse' do it 'assigns a newly created synapse as @synapse' do
post :create, synapse: valid_attributes, format: :json post :create, format: :json, params: {
expect(assigns(:synapse)).to be_a(Synapse) synapse: valid_attributes
expect(assigns(:synapse)).to be_persisted }
expect(comparable(Synapse.last)).to eq comparable(synapse)
end end
it 'returns 201 CREATED' do it 'returns 201 CREATED' do
post :create, synapse: valid_attributes, format: :json post :create, format: :json, params: {
synapse: valid_attributes
}
expect(response.status).to eq 201 expect(response.status).to eq 201
end end
end end
context 'with invalid params' do context 'with invalid params' do
it 'returns 422 UNPROCESSABLE ENTITY' do it 'returns 422 UNPROCESSABLE ENTITY' do
post :create, synapse: invalid_attributes, format: :json post :create, format: :json, params: {
synapse: invalid_attributes
}
expect(response.status).to eq 422 expect(response.status).to eq 422
end end
end end
@ -53,8 +53,9 @@ RSpec.describe SynapsesController, type: :controller do
end end
it 'updates the requested synapse' do it 'updates the requested synapse' do
put :update, put :update, format: :json, params: {
id: synapse.to_param, synapse: new_attributes, format: :json id: synapse.to_param, synapse: new_attributes
}
synapse.reload synapse.reload
expect(synapse.desc).to eq 'My new description' expect(synapse.desc).to eq 'My new description'
expect(synapse.category).to eq 'both' expect(synapse.category).to eq 'both'
@ -62,17 +63,19 @@ RSpec.describe SynapsesController, type: :controller do
end end
it 'returns 204 NO CONTENT' do it 'returns 204 NO CONTENT' do
put :update, put :update, format: :json, params: {
id: synapse.to_param, synapse: valid_attributes, format: :json id: synapse.to_param, synapse: valid_attributes
}
expect(response.status).to eq 204 expect(response.status).to eq 204
end end
end end
context 'with invalid params' do context 'with invalid params' do
it 'assigns the synapse as @synapse' do it 'assigns the synapse as @synapse' do
put :update, put :update, format: :json, params: {
id: synapse.to_param, synapse: invalid_attributes, format: :json id: synapse.to_param, synapse: invalid_attributes
expect(assigns(:synapse)).to eq(synapse) }
expect(Synapse.last).to eq synapse
end end
end end
end end
@ -83,12 +86,16 @@ RSpec.describe SynapsesController, type: :controller do
it 'destroys the requested synapse' do it 'destroys the requested synapse' do
synapse.reload # ensure it's present synapse.reload # ensure it's present
expect do expect do
delete :destroy, id: synapse.to_param, format: :json delete :destroy, format: :json, params: {
id: synapse.to_param
}
end.to change(Synapse, :count).by(-1) end.to change(Synapse, :count).by(-1)
end end
it 'returns 204 NO CONTENT' do it 'returns 204 NO CONTENT' do
delete :destroy, id: synapse.to_param, format: :json delete :destroy, format: :json, params: {
id: synapse.to_param
}
expect(response.status).to eq 204 expect(response.status).to eq 204
end end
end end

View file

@ -5,14 +5,7 @@ RSpec.describe TopicsController, type: :controller do
let(:valid_attributes) { topic.attributes.except('id') } let(:valid_attributes) { topic.attributes.except('id') }
let(:invalid_attributes) { { permission: :invalid_lol } } let(:invalid_attributes) { { permission: :invalid_lol } }
before :each do before :each do
sign_in sign_in create(:user)
end
describe 'GET #show' do
it 'assigns the requested topic as @topic' do
get :show, id: topic.to_param, format: :json
expect(assigns(:topic)).to eq(topic)
end
end end
describe 'POST #create' do describe 'POST #create' do
@ -20,26 +13,33 @@ RSpec.describe TopicsController, type: :controller do
it 'creates a new Topic' do it 'creates a new Topic' do
topic.reload # ensure it's created topic.reload # ensure it's created
expect do expect do
post :create, topic: valid_attributes, format: :json post :create, format: :json, params: {
topic: valid_attributes
}
end.to change(Topic, :count).by(1) end.to change(Topic, :count).by(1)
end end
it 'assigns a newly created topic as @topic' do it 'assigns a newly created topic as @topic' do
post :create, topic: valid_attributes, format: :json post :create, format: :json, params: {
expect(assigns(:topic)).to be_a(Topic) topic: valid_attributes
expect(assigns(:topic)).to be_persisted }
expect(comparable(Topic.last)).to eq comparable(topic)
end end
it 'returns 201 CREATED' do it 'returns 201 CREATED' do
post :create, topic: valid_attributes, format: :json post :create, format: :json, params: {
topic: valid_attributes
}
expect(response.status).to eq 201 expect(response.status).to eq 201
end end
end end
context 'with invalid params' do context 'with invalid params' do
it 'assigns a newly created but unsaved topic as @topic' do it 'assigns a newly created but unsaved topic as @topic' do
post :create, topic: invalid_attributes, format: :json post :create, format: :json, params: {
expect(assigns(:topic)).to be_a_new(Topic) topic: invalid_attributes
}
expect(Topic.count).to eq 0
end end
end end
end end
@ -54,8 +54,9 @@ RSpec.describe TopicsController, type: :controller do
end end
it 'updates the requested topic' do it 'updates the requested topic' do
put :update, put :update, format: :json, params: {
id: topic.to_param, topic: new_attributes, format: :json id: topic.to_param, topic: new_attributes
}
topic.reload topic.reload
expect(topic.name).to eq 'Cool Topic with no number' expect(topic.name).to eq 'Cool Topic with no number'
expect(topic.desc).to eq 'This is a cool topic.' expect(topic.desc).to eq 'This is a cool topic.'
@ -64,23 +65,26 @@ RSpec.describe TopicsController, type: :controller do
end end
it 'assigns the requested topic as @topic' do it 'assigns the requested topic as @topic' do
put :update, put :update, format: :json, params: {
id: topic.to_param, topic: valid_attributes, format: :json id: topic.to_param, topic: valid_attributes
expect(assigns(:topic)).to eq(topic) }
expect(Topic.last).to eq(topic)
end end
it 'returns status of no content' do it 'returns status of no content' do
put :update, put :update, format: :json, params: {
id: topic.to_param, topic: valid_attributes, format: :json id: topic.to_param, topic: valid_attributes
}
expect(response.status).to eq 204 expect(response.status).to eq 204
end end
end end
context 'with invalid params' do context 'with invalid params' do
it 'assigns the topic as @topic' do it 'assigns the topic as @topic' do
put :update, put :update, format: :json, params: {
id: topic.to_param, topic: invalid_attributes, format: :json id: topic.to_param, topic: invalid_attributes
expect(assigns(:topic)).to eq(topic) }
expect(Topic.last).to eq topic
end end
end end
end end
@ -90,7 +94,9 @@ RSpec.describe TopicsController, type: :controller do
it 'destroys the requested topic' do it 'destroys the requested topic' do
owned_topic.reload # ensure it's there owned_topic.reload # ensure it's there
expect do expect do
delete :destroy, id: owned_topic.to_param, format: :json delete :destroy, format: :json, params: {
id: owned_topic.to_param
}
end.to change(Topic, :count).by(-1) end.to change(Topic, :count).by(-1)
expect(response.body).to eq '' expect(response.body).to eq ''
expect(response.status).to eq 204 expect(response.status).to eq 204

View file

@ -6,5 +6,6 @@ FactoryGirl.define do
association :topic1, factory: :topic association :topic1, factory: :topic
association :topic2, factory: :topic association :topic2, factory: :topic
user user
weight 1 # todo drop this column
end end
end end

View file

@ -1,8 +1,10 @@
FactoryGirl.define do FactoryGirl.define do
factory :topic do factory :topic do
sequence(:name) { |n| "Cool Topic ##{n}" }
permission :commons
user user
metacode metacode
permission :commons
sequence(:name) { |n| "Cool Topic ##{n}" }
sequence(:desc) { |n| "topic desc #{n}" }
sequence(:link) { |n| "https://metamaps.cc/maps/#{n}" }
end end
end end

View file

@ -1,5 +0,0 @@
require 'rails_helper'
RSpec.describe MapMailer, type: :mailer do
pending "add some examples to (or delete) #{__FILE__}"
end

View file

@ -1,45 +1,26 @@
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test' ENV['RAILS_ENV'] ||= 'test'
require 'spec_helper'
require File.expand_path('../../config/environment', __FILE__) require File.expand_path('../../config/environment', __FILE__)
require 'rspec/rails'
# Prevent database truncation if the environment is production # Prevent database truncation if the environment is production
if Rails.env.production? if Rails.env.production?
abort('The Rails environment is running in production mode!') abort('The Rails environment is running in production mode!')
end end
require 'spec_helper'
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!
# require all support files # require all support files
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config| RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures" config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true config.use_transactional_fixtures = true
# RSpec Rails can automatically mix in different behaviours to your tests
# based on their file location, for example enabling you to call `get` and
# `post` in specs under `spec/controllers`.
#
# You can disable this behaviour by removing the line below, and instead
# explicitly tag your specs with their type, e.g.:
#
# RSpec.describe UsersController, :type => :controller do
# # ...
# end
#
# The different available types are documented in the features, such as in
# https://relishapp.com/rspec/rspec-rails/docs
config.infer_spec_type_from_file_location! config.infer_spec_type_from_file_location!
config.include Devise::TestHelpers, type: :controller
config.include ControllerHelpers, type: :controller
config.include Shoulda::Matchers::ActiveModel, type: :model config.include Shoulda::Matchers::ActiveModel, type: :model
config.include Shoulda::Matchers::ActiveRecord, type: :model config.include Shoulda::Matchers::ActiveRecord, type: :model
end end

View file

@ -1,7 +1,3 @@
require 'simplecov'
require 'support/controller_helpers'
require 'pundit/rspec'
RSpec.configure do |config| RSpec.configure do |config|
config.expect_with :rspec do |expectations| config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true expectations.include_chain_clauses_in_custom_matcher_descriptions = true

View file

@ -3,24 +3,17 @@
require 'devise' require 'devise'
module ControllerHelpers module ControllerHelpers
# rubocop:disable Metrics/AbcSize extend ActiveSupport::Concern
def sign_in(user = create(:user))
if user.nil? # simulate unauthenticated included do
allow(request.env['warden']).to( include Devise::Test::ControllerHelpers
receive(:authenticate!).and_throw(:warden, scope: :user) end
)
else # simulate authenticated def comparable(model)
allow_message_expectations_on_nil model.attributes.except('id', 'created_at', 'updated_at')
allow(request.env['warden']).to(
receive(:authenticate!).and_return(user)
)
end
allow(controller).to receive(:current_user).and_return(user)
end end
# rubocop:enable Metrics/AbcSize
end end
RSpec.configure do |config| RSpec.configure do |config|
config.include Devise::TestHelpers, type: :controller
config.include ControllerHelpers, type: :controller config.include ControllerHelpers, type: :controller
end end

1
spec/support/pundit.rb Normal file
View file

@ -0,0 +1 @@
require 'pundit/rspec'

View file

@ -0,0 +1,2 @@
require 'simplecov'