Merge pull request #456 from metamaps/feature/rspec-tests

rspec tests
This commit is contained in:
Devin Howard 2016-01-07 16:09:40 +08:00
commit a06a80eb43
76 changed files with 1566 additions and 702 deletions

2
.rspec Normal file
View file

@ -0,0 +1,2 @@
--color
--require spec_helper

View file

@ -38,6 +38,12 @@ group :assets do
gem 'uglifier'
end
group :test do
gem 'rspec-rails'
gem 'factory_girl_rails'
gem 'shoulda-matchers'
end
group :production do #this is used on heroku
#gem 'rmagick'
gem 'rails_12factor'

View file

@ -53,9 +53,9 @@ GEM
binding_of_caller (0.7.2)
debug_inspector (>= 0.0.1)
builder (3.2.2)
byebug (4.0.5)
byebug (5.0.0)
columnize (= 0.9.0)
cancancan (1.12.0)
cancancan (1.13.1)
climate_control (0.0.3)
activesupport (>= 3.0)
cocaine (0.5.7)
@ -77,10 +77,16 @@ GEM
responders
thread_safe (~> 0.1)
warden (~> 1.2.3)
dotenv (2.0.0)
diff-lcs (1.2.5)
dotenv (2.0.2)
erubis (2.7.0)
execjs (2.6.0)
ezcrypto (0.7.2)
factory_girl (4.5.0)
activesupport (>= 3.0.0)
factory_girl_rails (4.5.0)
factory_girl (~> 4.5.0)
railties (>= 3.0.0)
formtastic (3.1.3)
actionpack (>= 3.2.13)
formula (1.1.1)
@ -88,7 +94,7 @@ GEM
globalid (0.3.6)
activesupport (>= 4.1.0)
i18n (0.7.0)
jbuilder (2.3.1)
jbuilder (2.3.2)
activesupport (>= 3.0.0, < 5)
multi_json (~> 1.2)
jquery-rails (4.0.5)
@ -106,28 +112,28 @@ GEM
mail (2.6.3)
mime-types (>= 1.16, < 3)
method_source (0.8.2)
mime-types (2.6.1)
mime-types (2.6.2)
mimemagic (0.3.0)
mini_portile (0.6.2)
minitest (5.8.0)
minitest (5.8.2)
multi_json (1.11.2)
nokogiri (1.6.6.2)
mini_portile (~> 0.6.0)
oauth (0.4.7)
orm_adapter (0.5.0)
paperclip (4.3.0)
paperclip (4.3.1)
activemodel (>= 3.2.0)
activesupport (>= 3.2.0)
cocaine (~> 0.5.5)
mime-types
mimemagic (= 0.3.0)
pg (0.18.3)
pry (0.10.1)
pry (0.10.3)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
pry-byebug (3.1.0)
byebug (~> 4.0)
pry-byebug (3.2.0)
byebug (~> 5.0)
pry (~> 0.10)
pry-rails (0.3.4)
pry (>= 0.9.10)
@ -171,16 +177,35 @@ GEM
redis (3.2.1)
responders (2.1.0)
railties (>= 4.2.0, < 5)
sass (3.4.18)
rspec-core (3.3.2)
rspec-support (~> 3.3.0)
rspec-expectations (3.3.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.3.0)
rspec-mocks (3.3.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.3.0)
rspec-rails (3.3.3)
actionpack (>= 3.0, < 4.3)
activesupport (>= 3.0, < 4.3)
railties (>= 3.0, < 4.3)
rspec-core (~> 3.3.0)
rspec-expectations (~> 3.3.0)
rspec-mocks (~> 3.3.0)
rspec-support (~> 3.3.0)
rspec-support (3.3.0)
sass (3.4.19)
sass-rails (5.0.4)
railties (>= 4.0.0, < 5.0)
sass (~> 3.1)
sprockets (>= 2.8, < 4.0)
sprockets-rails (>= 2.0, < 4.0)
tilt (>= 1.1, < 3)
shoulda-matchers (3.0.1)
activesupport (>= 4.0.0)
slop (3.6.0)
sprockets (3.3.4)
rack (~> 1.0)
sprockets (3.4.0)
rack (> 1, < 3)
sprockets-rails (2.3.3)
actionpack (>= 3.0)
activesupport (>= 3.0)
@ -213,6 +238,7 @@ DEPENDENCIES
coffee-rails
devise
dotenv
factory_girl_rails
formtastic
formula
jbuilder
@ -229,7 +255,9 @@ DEPENDENCIES
rails3-jquery-autocomplete
rails_12factor
redis
rspec-rails
sass-rails
shoulda-matchers
tunemygc
uglifier
uservoice-ruby

View file

@ -60,13 +60,13 @@ private
end
def admin?
current_user && current_user.admin
authenticated? && current_user.admin
end
def get_invite_link
unsafe_uri = request.env["REQUEST_URI"]
unsafe_uri = request.env["REQUEST_URI"] || 'https://metamaps.cc'
valid_url = /^https?:\/\/([\w\.-]+)(:\d{1,5})?\/?$/
safe_uri = (unsafe_uri.match(valid_url)) ? unsafe_uri : "//metamaps.cc/"
safe_uri = (unsafe_uri.match(valid_url)) ? unsafe_uri : '//metamaps.cc/'
@invite_link = "#{safe_uri}join" + (current_user ? "?code=#{current_user.code}" : "")
end
end

View file

@ -1,5 +1,4 @@
class MetacodesController < ApplicationController
before_filter :require_admin, except: [:index]
# GET /metacodes

View file

@ -101,5 +101,4 @@ class UsersController < ApplicationController
def user_params
params.require(:user).permit(:name, :email, :image, :password, :password_confirmation)
end
end

View file

@ -13,6 +13,10 @@ class Map < ActiveRecord::Base
#:full => ['940x630#', :png]
},
:default_url => 'https://s3.amazonaws.com/metamaps-assets/site/missing-map.png'
validates :name, presence: true
validates :arranged, inclusion: { in: [true, false] }
validates :permission, presence: true
validates :permission, inclusion: { in: Perm::ISSIONS.map(&:to_s) }
# Validate the attached image is image/jpg, image/png, etc
validates_attachment_content_type :screenshot, :content_type => /\Aimage\/.*\Z/
@ -22,13 +26,7 @@ class Map < ActiveRecord::Base
end
def mk_permission
if self.permission == "commons"
"co"
elsif self.permission == "public"
"pu"
elsif self.permission == "private"
"pr"
end
Perm.short(permission)
end
#return an array of the contributors to the map
@ -109,14 +107,6 @@ class Map < ActiveRecord::Base
end
return self
end
# returns Boolean if user allowed to view Topic, Synapse, or Map
def authorize_to_view(user)
if (self.permission == "private" && self.user != user)
return false
end
return true
end
def decode_base64(imgBase64)
decoded_data = Base64.decode64(imgBase64)

View file

@ -10,6 +10,9 @@ class Synapse < ActiveRecord::Base
validates :desc, length: { minimum: 0, allow_nil: false }
validates :permission, presence: true
validates :permission, inclusion: { in: Perm::ISSIONS.map(&:to_s) }
def user_name
self.user.name
end
@ -48,13 +51,4 @@ class Synapse < ActiveRecord::Base
end
return false
end
# returns Boolean if user allowed to view Topic, Synapse, or Map
def authorize_to_view(user)
if (self.permission == "private" && self.user != user)
return false
end
return true
end
end

View file

@ -10,6 +10,9 @@ class Topic < ActiveRecord::Base
has_many :mappings, as: :mappable, dependent: :destroy
has_many :maps, :through => :mappings
validates :permission, presence: true
validates :permission, inclusion: { in: Perm::ISSIONS.map(&:to_s) }
# This method associates the attribute ":image" with a file attachment
has_attached_file :image
@ -39,27 +42,27 @@ class Topic < ActiveRecord::Base
belongs_to :metacode
def user_name
self.user.name
user.name
end
def user_image
self.user.image.url
user.image.url
end
def map_count
self.maps.count
maps.count
end
def synapse_count
self.synapses.count
synapses.count
end
def inmaps
self.maps.map(&:name)
maps.map(&:name)
end
def inmapsLinks
self.maps.map(&:id)
maps.map(&:id)
end
def as_json(options={})
@ -71,24 +74,18 @@ class Topic < ActiveRecord::Base
end
def mk_permission
if self.permission == "commons"
"co"
elsif self.permission == "public"
"pu"
elsif self.permission == "private"
"pr"
end
Perm.short(permission)
end
# has no viewable synapses helper function
def has_viewable_synapses(current)
result = false
self.synapses.each do |synapse|
if synapse.authorize_to_view(current)
synapses.each do |synapse|
if synapse.authorize_to_show(current)
result = true
end
end
return result
result
end
##### PERMISSIONS ######
@ -117,12 +114,4 @@ class Topic < ActiveRecord::Base
end
return false
end
# returns Boolean if user allowed to view Topic, Synapse, or Map
def authorize_to_view(user)
if (self.permission == "private" && self.user != user)
return false
end
return true
end
end

View file

@ -64,7 +64,7 @@ class User < ActiveRecord::Base
#generate a random 8 letter/digit code that they can use to invite people
def generate_code
self.code = rand(36**8).to_s(36)
self.code ||= rand(36**8).to_s(36)
$codes.push(self.code)
self.generation = get_generation!
end

37
app/services/perm.rb Normal file
View file

@ -0,0 +1,37 @@
class Perm
# e.g. Perm::ISSIONS
ISSIONS = [:commons, :public, :private]
class << self
def short(permission)
case permission
when :commons
"co"
when :public
"pu"
when :private
"pr"
else
fail "Invalid permission"
end
end
def long(perm)
case perm
when "co"
:commons
when "pu"
:public
when "pr"
:private
else
fail "Invalid short permission"
end
end
def valid?(permission)
ISSIONS.include? permission
end
end
end

View file

@ -11,7 +11,7 @@ module Metamaps
# -- all .rb files in that directory are automatically loaded.
# Custom directories with classes and modules you want to be autoloadable.
# config.autoload_paths += %W(#{config.root}/extras)
config.autoload_paths << Rails.root.join('app', 'services')
# Only load the plugins named here, in the order given (default is alphabetical).
# :all can be used as a placeholder for all plugins not explicitly named.
@ -45,6 +45,9 @@ module Metamaps
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '2.0'
config.generators do |g|
g.test_framework :rspec
end
config.active_record.raise_in_transactional_callbacks = true
end
end

View file

@ -10,7 +10,7 @@ Metamaps::Application.configure do
config.cache_classes = true
# Configure static asset server for tests with Cache-Control for performance
config.serve_static_assets = true
config.serve_static_files = true
config.static_cache_control = "public, max-age=3600"
# Show full error reports and disable caching

View file

@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20151025083043) do
ActiveRecord::Schema.define(version: 20151028061513) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

View file

@ -1,7 +1,306 @@
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
#
# Examples:
#
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
# Mayor.create(name: 'Emanuel', city: cities.first)
## USERS
User.new({
name: 'user',
email: 'user@user.com',
password: 'toolsplusconsciousness',
code: 'qwertyui',
joinedwithcode: 'qwertyui',
admin: 'false'
}).save
User.new({
name: 'admin',
email: 'admin@admin.com',
password: 'toolsplusconsciousness',
code: 'iuytrewq',
joinedwithcode: 'iuytrewq',
admin: 'true'
}).save
## END USERS
## METACODES
Metacode.create({
name: 'Action',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_action.png',
color: '#BD6C85'
})
Metacode.create({
name: 'Activity',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_activity.png',
color: '#6EBF65'
})
Metacode.create({
name: 'Catalyst',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_catalyst.png',
color: '#EF8964',
})
Metacode.create({
name: 'Closed',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_closedissue.png',
color: '#ABB49F',
})
Metacode.create({
name: 'Process',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_process.png',
color: '#BDB25E',
})
Metacode.create({
name: 'Future',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_futuredev.png',
color: '#25A17F',
})
Metacode.create({
name: 'Group',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_group.png',
color: '#7076BC',
})
Metacode.create({
name: 'Implication',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_implication.png',
color: '#83DECA',
})
Metacode.create({
name: 'Insight',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_insight.png',
color: '#B074AD',
})
Metacode.create({
name: 'Intention',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_intention.png',
color: '#BAEAFF',
})
Metacode.create({
name: 'Knowledge',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_knowledge.png',
color: '#60ACF7',
})
Metacode.create({
name: 'Location',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_location.png',
color: '#ABD9A7',
})
Metacode.create({
name: 'Need',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_need.png',
color: '#D2A7D4',
})
Metacode.create({
name: 'Open',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_openissue.png',
color: '#9BBF71',
})
Metacode.create({
name: 'Opportunity',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_opportunity.png',
color: '#889F64',
})
Metacode.create({
name: 'Person',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_person.png',
color: '#DE925F',
})
Metacode.create({
name: 'Platform',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_platform.png',
color: '#21C8FE',
})
Metacode.create({
name: 'Problem',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_problem.png',
color: '#99CFC4',
})
Metacode.create({
name: 'Resource',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_resource.png',
color: '#C98C63',
})
Metacode.create({
name: 'Role',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_role.png',
color: '#A8595D',
})
Metacode.create({
name: 'Task',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_task.png',
color: '#3397C4',
})
Metacode.create({
name: 'Trajectory',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_trajectory.png',
color: '#D3AA4C',
})
Metacode.create({
name: 'Argument',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_argument.png',
color: '#7FAEFD',
})
Metacode.create({
name: 'Con',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_con.png',
color: '#CF7C74',
})
Metacode.create({
name: 'Subject',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_subject.png',
color: '#8293D8',
})
Metacode.create({
name: 'Decision',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_decision.png',
color: '#CCA866',
})
Metacode.create({
name: 'Event',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_event.png',
color: '#F5854B',
})
Metacode.create({
name: 'Example',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_example.png',
color: '#618C61',
})
Metacode.create({
name: 'Experience',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_experience.png',
color: '#BE995F',
})
Metacode.create({
name: 'Feedback',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_feedback.png',
color: '#54A19D',
})
Metacode.create({
name: 'Aim',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_aim.png',
color: '#B0B0B0',
})
Metacode.create({
name: 'Good',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_goodpractice.png',
color: '#BD9E86',
})
Metacode.create({
name: 'Idea',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_idea.png',
color: '#C4BC5E',
})
Metacode.create({
name: 'List',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_list.png',
color: '#B7A499',
})
Metacode.create({
name: 'Media',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_media.png',
color: '#6D94CC',
})
Metacode.create({
name: 'Metamap',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_metamap.png',
color: '#AEA9FD',
})
Metacode.create({
name: 'Model',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_model.png',
color: '#B385BA',
})
Metacode.create({
name: 'Note',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_note.png',
color: '#A389A1',
})
Metacode.create({
name: 'Perspective',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_perspective.png',
color: '#2EB6CC',
})
Metacode.create({
name: 'Pro',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_pro.png',
color: '#89B879',
})
Metacode.create({
name: 'Project',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_project.png',
color: '#85A050',
})
Metacode.create({
name: 'Question',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_question.png',
color: '#5CB3B3',
})
Metacode.create({
name: 'Reference',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_reference.png',
color: '#A7A7A7',
})
Metacode.create({
name: 'Research',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_research.png',
color: '#CD8E89',
})
Metacode.create({
name: 'Status',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_status.png',
color: '#EFA7C0',
})
Metacode.create({
name: 'Tool',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_tool.png',
color: '#828282',
})
Metacode.create({
name: 'Wildcard',
icon: 'https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_wildcard.png',
color: '#73C7DE',
})
## END METACODES

53
doc/RspecTesting.md Normal file
View file

@ -0,0 +1,53 @@
## Testing with RSpec
RSpec is a ruby gem that allows you to test your code base. This is great -
every time you make a change, you can do some basic sanity checks to make sure
you didn't break anything.
To test Metamaps, run
rspec
in the top level directory. It will automatically search the `spec` directory
for files called `*_spec.rb`, and run them as tests. When it's done testing, it
will print a report telling you how many tests failed. With luck, the number
will be 0.
Note that if your test database doesn't exist yet, you'll need to create it
first:
RAILS_ENV=test rake db:create
At the time of writing, there are four directories in the spec folder. One,
`support`, is for helper functions. `rails_helper.rb` and `spec_helper.rb` are
also for helper functions.
`factories` is for a gem called [factory-girl][factory-girl]. This gem lets you
use the `create` and `build` functions to quickly create the simplest possible
valid version of a given model. For instance:
let(:map1) { create :map }
let(:ronald) { create :user, name: "Ronald" }
let(:map2) { create :map, user: ronald }
As you can see, you can also customize the factories. You can read the full
documentation at the link above or check the existing specs to see how it works.
It is worth reading through the factories to see how they are defined. If you
add a model to `app/models`, please also create a factory for it that defines
the minimum valid state for that model.
Finally, `models` and `controllers` have the actual spec files. Writing specs is
usually fairly simple but you do need to understand the syntax. You can read
more at [rspec.info][rspec-docs].
If you modify the metamaps codebase, please consider adding tests verifying that
the added code works. This will help in a few ways:
- Unrelated changes in the future that break your code will be spotted earlier
- Your changes will be more easily understood, since the *purpose* will be
described by the spec
Happy testing!
[factory-girl]: https://github.com/thoughtbot/factory_girl
[rspec-docs]: http://rspec.info

View file

@ -0,0 +1,160 @@
require 'rails_helper'
# This spec was generated by rspec-rails when you ran the scaffold generator.
# It demonstrates how one might use RSpec to specify the controller code that
# was generated by Rails when you ran the scaffold generator.
#
# It assumes that the implementation code is generated by the rails scaffold
# generator. If you are using any extension libraries to generate different
# controller code, this generated spec may or may not pass.
#
# It only uses APIs available in rails and/or rspec-rails. There are a number
# of tools you can use to make these specs even more expressive, but we're
# sticking to rails and rspec-rails APIs to keep things simple and stable.
#
# Compared to earlier versions of this generator, there is very limited use of
# stubs and message expectations in this spec. Stubs are only used when there
# is no simpler way to get a handle on the object needed for the example.
# Message expectations are only used when there is no simpler way to specify
# that an instance is receiving a specific message.
RSpec.describe MappingsController, type: :controller do
# This should return the minimal set of attributes required to create a valid
# Mapping. As you add validations to Mapping, be sure to
# adjust the attributes here as well.
let(:valid_attributes) do
skip('Add a hash of attributes valid for your model')
end
let(:invalid_attributes) do
skip('Add a hash of attributes invalid for your model')
end
# This should return the minimal set of values that should be in the session
# in order to pass any filters (e.g. authentication) defined in
# MappingsController. Be sure to keep this updated too.
let(:valid_session) { {} }
describe 'GET #index' do
it 'assigns all mappings as @mappings' do
mapping = Mapping.create! valid_attributes
get :index, {}, valid_session
expect(assigns(:mappings)).to eq([mapping])
end
end
describe 'GET #show' do
it 'assigns the requested mapping as @mapping' do
mapping = Mapping.create! valid_attributes
get :show, { id: mapping.to_param }, valid_session
expect(assigns(:mapping)).to eq(mapping)
end
end
describe 'GET #edit' do
it 'assigns the requested mapping as @mapping' do
mapping = Mapping.create! valid_attributes
get :edit, { id: mapping.to_param }, valid_session
expect(assigns(:mapping)).to eq(mapping)
end
end
describe 'POST #create' do
context 'with valid params' do
it 'creates a new Mapping' do
expect do
post :create, { mapping: valid_attributes }, valid_session
end.to change(Mapping, :count).by(1)
end
it 'assigns a newly created mapping as @mapping' do
post :create, { mapping: valid_attributes }, valid_session
expect(assigns(:mapping)).to be_a(Mapping)
expect(assigns(:mapping)).to be_persisted
end
it 'redirects to the created mapping' do
post :create, { mapping: valid_attributes }, valid_session
expect(response).to redirect_to(Mapping.last)
end
end
context 'with invalid params' do
it 'assigns a newly created but unsaved mapping as @mapping' do
post :create, { mapping: invalid_attributes }, valid_session
expect(assigns(:mapping)).to be_a_new(Mapping)
end
it "re-renders the 'new' template" do
post :create, { mapping: invalid_attributes }, valid_session
expect(response).to render_template('new')
end
end
end
describe 'PUT #update' do
context 'with valid params' do
let(:new_attributes) do
skip('Add a hash of attributes valid for your model')
end
it 'updates the requested mapping' do
mapping = Mapping.create! valid_attributes
put :update,
{ id: mapping.to_param, mapping: new_attributes },
valid_session
mapping.reload
skip('Add assertions for updated state')
end
it 'assigns the requested mapping as @mapping' do
mapping = Mapping.create! valid_attributes
put :update,
{ id: mapping.to_param, mapping: valid_attributes },
valid_session
expect(assigns(:mapping)).to eq(mapping)
end
it 'redirects to the mapping' do
mapping = Mapping.create! valid_attributes
put :update,
{ id: mapping.to_param, mapping: valid_attributes },
valid_session
expect(response).to redirect_to(mapping)
end
end
context 'with invalid params' do
it 'assigns the mapping as @mapping' do
mapping = Mapping.create! valid_attributes
put :update,
{ id: mapping.to_param, mapping: invalid_attributes },
valid_session
expect(assigns(:mapping)).to eq(mapping)
end
it "re-renders the 'edit' template" do
mapping = Mapping.create! valid_attributes
put :update,
{ id: mapping.to_param, mapping: invalid_attributes },
valid_session
expect(response).to render_template('edit')
end
end
end
describe 'DELETE #destroy' do
it 'destroys the requested mapping' do
mapping = Mapping.create! valid_attributes
expect do
delete :destroy, { id: mapping.to_param }, valid_session
end.to change(Mapping, :count).by(-1)
end
it 'redirects to the mappings list' do
mapping = Mapping.create! valid_attributes
delete :destroy, { id: mapping.to_param }, valid_session
expect(response).to redirect_to(mappings_url)
end
end
end

View file

@ -0,0 +1,160 @@
require 'rails_helper'
# This spec was generated by rspec-rails when you ran the scaffold generator.
# It demonstrates how one might use RSpec to specify the controller code that
# was generated by Rails when you ran the scaffold generator.
#
# It assumes that the implementation code is generated by the rails scaffold
# generator. If you are using any extension libraries to generate different
# controller code, this generated spec may or may not pass.
#
# It only uses APIs available in rails and/or rspec-rails. There are a number
# of tools you can use to make these specs even more expressive, but we're
# sticking to rails and rspec-rails APIs to keep things simple and stable.
#
# Compared to earlier versions of this generator, there is very limited use of
# stubs and message expectations in this spec. Stubs are only used when there
# is no simpler way to get a handle on the object needed for the example.
# Message expectations are only used when there is no simpler way to specify
# that an instance is receiving a specific message.
RSpec.describe MapsController, type: :controller do
# This should return the minimal set of attributes required to create a valid
# Map. As you add validations to Map, be sure to
# adjust the attributes here as well.
let(:valid_attributes) do
skip('Add a hash of attributes valid for your model')
end
let(:invalid_attributes) do
skip('Add a hash of attributes invalid for your model')
end
# This should return the minimal set of values that should be in the session
# in order to pass any filters (e.g. authentication) defined in
# MapsController. Be sure to keep this updated too.
let(:valid_session) { {} }
describe 'GET #index' do
it 'assigns all maps as @maps' do
map = Map.create! valid_attributes
get :index, {}, valid_session
expect(assigns(:maps)).to eq([map])
end
end
describe 'GET #show' do
it 'assigns the requested map as @map' do
map = Map.create! valid_attributes
get :show, { id: map.to_param }, valid_session
expect(assigns(:map)).to eq(map)
end
end
describe 'GET #edit' do
it 'assigns the requested map as @map' do
map = Map.create! valid_attributes
get :edit, { id: map.to_param }, valid_session
expect(assigns(:map)).to eq(map)
end
end
describe 'POST #create' do
context 'with valid params' do
it 'creates a new Map' do
expect do
post :create, { map: valid_attributes }, valid_session
end.to change(Map, :count).by(1)
end
it 'assigns a newly created map as @map' do
post :create, { map: valid_attributes }, valid_session
expect(assigns(:map)).to be_a(Map)
expect(assigns(:map)).to be_persisted
end
it 'redirects to the created map' do
post :create, { map: valid_attributes }, valid_session
expect(response).to redirect_to(Map.last)
end
end
context 'with invalid params' do
it 'assigns a newly created but unsaved map as @map' do
post :create, { map: invalid_attributes }, valid_session
expect(assigns(:map)).to be_a_new(Map)
end
it "re-renders the 'new' template" do
post :create, { map: invalid_attributes }, valid_session
expect(response).to render_template('new')
end
end
end
describe 'PUT #update' do
context 'with valid params' do
let(:new_attributes) do
skip('Add a hash of attributes valid for your model')
end
it 'updates the requested map' do
map = Map.create! valid_attributes
put :update,
{ id: map.to_param, map: new_attributes },
valid_session
map.reload
skip('Add assertions for updated state')
end
it 'assigns the requested map as @map' do
map = Map.create! valid_attributes
put :update,
{ id: map.to_param, map: valid_attributes },
valid_session
expect(assigns(:map)).to eq(map)
end
it 'redirects to the map' do
map = Map.create! valid_attributes
put :update,
{ id: map.to_param, map: valid_attributes },
valid_session
expect(response).to redirect_to(map)
end
end
context 'with invalid params' do
it 'assigns the map as @map' do
map = Map.create! valid_attributes
put :update,
{ id: map.to_param, map: invalid_attributes },
valid_session
expect(assigns(:map)).to eq(map)
end
it "re-renders the 'edit' template" do
map = Map.create! valid_attributes
put :update,
{ id: map.to_param, map: invalid_attributes },
valid_session
expect(response).to render_template('edit')
end
end
end
describe 'DELETE #destroy' do
it 'destroys the requested map' do
map = Map.create! valid_attributes
expect do
delete :destroy, { id: map.to_param }, valid_session
end.to change(Map, :count).by(-1)
end
it 'redirects to the maps list' do
map = Map.create! valid_attributes
delete :destroy, { id: map.to_param }, valid_session
expect(response).to redirect_to(maps_url)
end
end
end

View file

@ -0,0 +1,172 @@
require 'rails_helper'
# This spec was generated by rspec-rails when you ran the scaffold generator.
# It demonstrates how one might use RSpec to specify the controller code that
# was generated by Rails when you ran the scaffold generator.
#
# It assumes that the implementation code is generated by the rails scaffold
# generator. If you are using any extension libraries to generate different
# controller code, this generated spec may or may not pass.
#
# It only uses APIs available in rails and/or rspec-rails. There are a number
# of tools you can use to make these specs even more expressive, but we're
# sticking to rails and rspec-rails APIs to keep things simple and stable.
#
# Compared to earlier versions of this generator, there is very limited use of
# stubs and message expectations in this spec. Stubs are only used when there
# is no simpler way to get a handle on the object needed for the example.
# Message expectations are only used when there is no simpler way to specify
# that an instance is receiving a specific message.
RSpec.describe MetacodesController, type: :controller do
before :each do
@user = create(:user, admin: true)
sign_in @user
end
# This should return the minimal set of attributes required to create a valid
# Metacode. As you add validations to Metacode, be sure to
# adjust the attributes here as well.
let(:valid_attributes) do
skip('Add a hash of attributes valid for your model')
end
let(:invalid_attributes) do
skip('Add a hash of attributes invalid for your model')
end
# This should return the minimal set of values that should be in the session
# in order to pass any filters (e.g. authentication) defined in
# MetacodesController. Be sure to keep this updated too.
let(:valid_session) { {} }
describe 'GET #index' do
it 'assigns all metacodes as @metacodes' do
metacode = Metacode.create! valid_attributes
get :index, {}, valid_session
expect(assigns(:metacodes)).to eq([metacode])
end
end
describe 'GET #show' do
it 'assigns the requested metacode as @metacode' do
metacode = Metacode.create! valid_attributes
get :show, { id: metacode.to_param }, valid_session
expect(assigns(:metacode)).to eq(metacode)
end
end
describe 'GET #new' do
it 'assigns a new metacode as @metacode' do
get :new, {}, valid_session
expect(assigns(:metacode)).to be_a_new(Metacode)
end
end
describe 'GET #edit' do
it 'assigns the requested metacode as @metacode' do
metacode = Metacode.create! valid_attributes
get :edit, { id: metacode.to_param }, valid_session
expect(assigns(:metacode)).to eq(metacode)
end
end
describe 'POST #create' do
context 'with valid params' do
it 'creates a new Metacode' do
expect do
post :create, { metacode: valid_attributes }, valid_session
end.to change(Metacode, :count).by(1)
end
it 'assigns a newly created metacode as @metacode' do
post :create, { metacode: valid_attributes }, valid_session
expect(assigns(:metacode)).to be_a(Metacode)
expect(assigns(:metacode)).to be_persisted
end
it 'redirects to the created metacode' do
post :create, { metacode: valid_attributes }, valid_session
expect(response).to redirect_to(Metacode.last)
end
end
context 'with invalid params' do
it 'assigns a newly created but unsaved metacode as @metacode' do
post :create, { metacode: invalid_attributes }, valid_session
expect(assigns(:metacode)).to be_a_new(Metacode)
end
it "re-renders the 'new' template" do
post :create, { metacode: invalid_attributes }, valid_session
expect(response).to render_template('new')
end
end
end
describe 'PUT #update' do
context 'with valid params' do
let(:new_attributes) do
skip('Add a hash of attributes valid for your model')
end
it 'updates the requested metacode' do
metacode = Metacode.create! valid_attributes
put :update,
{ id: metacode.to_param, metacode: new_attributes },
valid_session
metacode.reload
skip('Add assertions for updated state')
end
it 'assigns the requested metacode as @metacode' do
metacode = Metacode.create! valid_attributes
put :update,
{ id: metacode.to_param, metacode: valid_attributes },
valid_session
expect(assigns(:metacode)).to eq(metacode)
end
it 'redirects to the metacode' do
metacode = Metacode.create! valid_attributes
put :update,
{ id: metacode.to_param, metacode: valid_attributes },
valid_session
expect(response).to redirect_to(metacode)
end
end
context 'with invalid params' do
it 'assigns the metacode as @metacode' do
metacode = Metacode.create! valid_attributes
put :update,
{ id: metacode.to_param, metacode: invalid_attributes },
valid_session
expect(assigns(:metacode)).to eq(metacode)
end
it "re-renders the 'edit' template" do
metacode = Metacode.create! valid_attributes
put :update,
{ id: metacode.to_param, metacode: invalid_attributes },
valid_session
expect(response).to render_template('edit')
end
end
end
describe 'DELETE #destroy' do
it 'destroys the requested metacode' do
metacode = Metacode.create! valid_attributes
expect do
delete :destroy, { id: metacode.to_param }, valid_session
end.to change(Metacode, :count).by(-1)
end
it 'redirects to the metacodes list' do
metacode = Metacode.create! valid_attributes
delete :destroy, { id: metacode.to_param }, valid_session
expect(response).to redirect_to(metacodes_url)
end
end
end

View file

@ -0,0 +1,160 @@
require 'rails_helper'
# This spec was generated by rspec-rails when you ran the scaffold generator.
# It demonstrates how one might use RSpec to specify the controller code that
# was generated by Rails when you ran the scaffold generator.
#
# It assumes that the implementation code is generated by the rails scaffold
# generator. If you are using any extension libraries to generate different
# controller code, this generated spec may or may not pass.
#
# It only uses APIs available in rails and/or rspec-rails. There are a number
# of tools you can use to make these specs even more expressive, but we're
# sticking to rails and rspec-rails APIs to keep things simple and stable.
#
# Compared to earlier versions of this generator, there is very limited use of
# stubs and message expectations in this spec. Stubs are only used when there
# is no simpler way to get a handle on the object needed for the example.
# Message expectations are only used when there is no simpler way to specify
# that an instance is receiving a specific message.
RSpec.describe SynapsesController, type: :controller do
# This should return the minimal set of attributes required to create a valid
# Synapse. As you add validations to Synapse, be sure to
# adjust the attributes here as well.
let(:valid_attributes) do
skip('Add a hash of attributes valid for your model')
end
let(:invalid_attributes) do
skip('Add a hash of attributes invalid for your model')
end
# This should return the minimal set of values that should be in the session
# in order to pass any filters (e.g. authentication) defined in
# SynapsesController. Be sure to keep this updated too.
let(:valid_session) { {} }
describe 'GET #index' do
it 'assigns all synapses as @synapses' do
synapse = Synapse.create! valid_attributes
get :index, {}, valid_session
expect(assigns(:synapses)).to eq([synapse])
end
end
describe 'GET #show' do
it 'assigns the requested synapse as @synapse' do
synapse = Synapse.create! valid_attributes
get :show, { id: synapse.to_param }, valid_session
expect(assigns(:synapse)).to eq(synapse)
end
end
describe 'GET #edit' do
it 'assigns the requested synapse as @synapse' do
synapse = Synapse.create! valid_attributes
get :edit, { id: synapse.to_param }, valid_session
expect(assigns(:synapse)).to eq(synapse)
end
end
describe 'POST #create' do
context 'with valid params' do
it 'creates a new Synapse' do
expect do
post :create, { synapse: valid_attributes }, valid_session
end.to change(Synapse, :count).by(1)
end
it 'assigns a newly created synapse as @synapse' do
post :create, { synapse: valid_attributes }, valid_session
expect(assigns(:synapse)).to be_a(Synapse)
expect(assigns(:synapse)).to be_persisted
end
it 'redirects to the created synapse' do
post :create, { synapse: valid_attributes }, valid_session
expect(response).to redirect_to(Synapse.last)
end
end
context 'with invalid params' do
it 'assigns a newly created but unsaved synapse as @synapse' do
post :create, { synapse: invalid_attributes }, valid_session
expect(assigns(:synapse)).to be_a_new(Synapse)
end
it "re-renders the 'new' template" do
post :create, { synapse: invalid_attributes }, valid_session
expect(response).to render_template('new')
end
end
end
describe 'PUT #update' do
context 'with valid params' do
let(:new_attributes) do
skip('Add a hash of attributes valid for your model')
end
it 'updates the requested synapse' do
synapse = Synapse.create! valid_attributes
put :update,
{ id: synapse.to_param, synapse: new_attributes },
valid_session
synapse.reload
skip('Add assertions for updated state')
end
it 'assigns the requested synapse as @synapse' do
synapse = Synapse.create! valid_attributes
put :update,
{ id: synapse.to_param, synapse: valid_attributes },
valid_session
expect(assigns(:synapse)).to eq(synapse)
end
it 'redirects to the synapse' do
synapse = Synapse.create! valid_attributes
put :update,
{ id: synapse.to_param, synapse: valid_attributes },
valid_session
expect(response).to redirect_to(synapse)
end
end
context 'with invalid params' do
it 'assigns the synapse as @synapse' do
synapse = Synapse.create! valid_attributes
put :update,
{ id: synapse.to_param, synapse: invalid_attributes },
valid_session
expect(assigns(:synapse)).to eq(synapse)
end
it "re-renders the 'edit' template" do
synapse = Synapse.create! valid_attributes
put :update,
{ id: synapse.to_param, synapse: invalid_attributes },
valid_session
expect(response).to render_template('edit')
end
end
end
describe 'DELETE #destroy' do
it 'destroys the requested synapse' do
synapse = Synapse.create! valid_attributes
expect do
delete :destroy, { id: synapse.to_param }, valid_session
end.to change(Synapse, :count).by(-1)
end
it 'redirects to the synapses list' do
synapse = Synapse.create! valid_attributes
delete :destroy, { id: synapse.to_param }, valid_session
expect(response).to redirect_to(synapses_url)
end
end
end

View file

@ -0,0 +1,136 @@
require 'rails_helper'
RSpec.describe TopicsController, type: :controller do
let(:valid_attributes) do
skip('Add a hash of attributes valid for your model')
end
let(:invalid_attributes) do
skip('Add a hash of attributes invalid for your model')
end
let(:valid_session) { {} }
describe 'GET #index' do
it 'assigns all topics as @topics' do
topic = Topic.create! valid_attributes
get :index, {}, valid_session
expect(assigns(:topics)).to eq([topic])
end
end
describe 'GET #show' do
it 'assigns the requested topic as @topic' do
topic = Topic.create! valid_attributes
get :show, { id: topic.to_param }, valid_session
expect(assigns(:topic)).to eq(topic)
end
end
describe 'GET #edit' do
it 'assigns the requested topic as @topic' do
topic = Topic.create! valid_attributes
get :edit, { id: topic.to_param }, valid_session
expect(assigns(:topic)).to eq(topic)
end
end
describe 'POST #create' do
context 'with valid params' do
it 'creates a new Topic' do
expect do
post :create, { topic: valid_attributes }, valid_session
end.to change(Topic, :count).by(1)
end
it 'assigns a newly created topic as @topic' do
post :create, { topic: valid_attributes }, valid_session
expect(assigns(:topic)).to be_a(Topic)
expect(assigns(:topic)).to be_persisted
end
it 'redirects to the created topic' do
post :create, { topic: valid_attributes }, valid_session
expect(response).to redirect_to(Topic.last)
end
end
context 'with invalid params' do
it 'assigns a newly created but unsaved topic as @topic' do
post :create, { topic: invalid_attributes }, valid_session
expect(assigns(:topic)).to be_a_new(Topic)
end
it "re-renders the 'new' template" do
post :create, { topic: invalid_attributes }, valid_session
expect(response).to render_template('new')
end
end
end
describe 'PUT #update' do
context 'with valid params' do
let(:new_attributes) do
skip('Add a hash of attributes valid for your model')
end
it 'updates the requested topic' do
topic = Topic.create! valid_attributes
put :update,
{ id: topic.to_param, topic: new_attributes },
valid_session
topic.reload
skip('Add assertions for updated state')
end
it 'assigns the requested topic as @topic' do
topic = Topic.create! valid_attributes
put :update,
{ id: topic.to_param, topic: valid_attributes },
valid_session
expect(assigns(:topic)).to eq(topic)
end
it 'redirects to the topic' do
topic = Topic.create! valid_attributes
put :update,
{ id: topic.to_param, topic: valid_attributes },
valid_session
expect(response).to redirect_to(topic)
end
end
context 'with invalid params' do
it 'assigns the topic as @topic' do
topic = Topic.create! valid_attributes
put :update,
{ id: topic.to_param, topic: invalid_attributes },
valid_session
expect(assigns(:topic)).to eq(topic)
end
it "re-renders the 'edit' template" do
topic = Topic.create! valid_attributes
put :update,
{ id: topic.to_param, topic: invalid_attributes },
valid_session
expect(response).to render_template('edit')
end
end
end
describe 'DELETE #destroy' do
it 'destroys the requested topic' do
topic = Topic.create! valid_attributes
expect do
delete :destroy, { id: topic.to_param }, valid_session
end.to change(Topic, :count).by(-1)
end
it 'redirects to the topics list' do
topic = Topic.create! valid_attributes
delete :destroy, { id: topic.to_param }, valid_session
expect(response).to redirect_to(topics_url)
end
end
end

8
spec/factories/maps.rb Normal file
View file

@ -0,0 +1,8 @@
FactoryGirl.define do
factory :map do
sequence(:name) { |n| "Cool Map ##{n}" }
permission :commons
arranged { false }
user
end
end

View file

@ -0,0 +1,4 @@
FactoryGirl.define do
factory :metacode do
end
end

View file

@ -0,0 +1,9 @@
FactoryGirl.define do
factory :synapse do
sequence(:desc) { |n| "Cool synapse ##{n}" }
category :to
permission :commons
association :topic1, factory: :topic
association :topic2, factory: :topic
end
end

8
spec/factories/topics.rb Normal file
View file

@ -0,0 +1,8 @@
FactoryGirl.define do
factory :topic do
sequence(:name) { |n| "Cool Topic ##{n}" }
permission :commons
user
metacode
end
end

29
spec/factories/users.rb Normal file
View file

@ -0,0 +1,29 @@
#
# This file supports three factories, because code and joinedwithcode
# make things complicated!
#
# Generally, use :simple_user to generate users. It's simplest.
#
# If you want to test code generation, or need a setup with users that
# have actual codes, you'll need to specify one simple_user and then you
# can specify other :code_user users based on the pre-existing user's code.
FactoryGirl.define do
factory :code_user, class: User do
sequence(:name) { |n| "Cool User ##{n}" }
sequence(:email) { |n| "cooluser#{n}@cooldomain.com" }
password 'omgwtfbbq'
factory :simple_user, aliases: [:user] do
joinedwithcode { "qwertyui" }
code { "qwertyui" }
transient do
validate false
end
# bypass validation of the joinedwithcode
to_create { |instance| instance.save(validate: instance.validate) }
end
end
end

38
spec/models/map_spec.rb Normal file
View file

@ -0,0 +1,38 @@
require 'rails_helper'
RSpec.describe Map, type: :model do
it { is_expected.to belong_to :user }
it { is_expected.to validate_presence_of :name }
it { is_expected.to validate_presence_of :permission }
it { is_expected.to validate_inclusion_of(:permission).in_array Perm::ISSIONS.map(&:to_s) }
context 'permissions' do
let(:owner) { create :user }
let(:other_user) { create :user }
let(:map) { create :map, user: owner, permission: :commons }
let(:private_map) { create :map, user: owner, permission: :private }
let(:public_map) { create :map, user: owner, permission: :public }
it 'prevents deletion by non-owner' do
expect(map.authorize_to_delete(other_user)).to eq false
expect(map.authorize_to_delete(owner)).to eq map
end
it 'prevents visibility if private' do
expect(map.authorize_to_show(other_user)).to eq map
expect(map.authorize_to_show(owner)).to eq map
expect(private_map.authorize_to_show(owner)).to eq private_map
expect(private_map.authorize_to_show(other_user)).to eq false
end
it 'only allows editing if commons or owned' do
expect(map.authorize_to_edit(other_user)).to eq map
expect(map.authorize_to_edit(owner)).to eq map
expect(private_map.authorize_to_edit(other_user)).to eq false
expect(private_map.authorize_to_edit(owner)).to eq private_map
expect(public_map.authorize_to_edit(other_user)).to eq false
expect(public_map.authorize_to_edit(owner)).to eq public_map
end
end
end

View file

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

View file

@ -0,0 +1,6 @@
require 'rails_helper'
RSpec.describe Metacode, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
it { is_expected.to have_many(:topics) }
end

View file

@ -0,0 +1,41 @@
require 'rails_helper'
RSpec.describe Synapse, type: :model do
it { is_expected.to belong_to :user }
it { is_expected.to belong_to :topic1 }
it { is_expected.to belong_to :topic2 }
it { is_expected.to have_many :maps }
it { is_expected.to have_many :mappings }
it { is_expected.to validate_presence_of :permission }
it { is_expected.to validate_inclusion_of(:permission).in_array Perm::ISSIONS.map(&:to_s) }
it { is_expected.to validate_length_of(:desc).is_at_least(0) } # TODO don't allow nil
context 'permissions' do
let(:owner) { create :user }
let(:other_user) { create :user }
let(:synapse) { create :synapse, user: owner, permission: :commons }
let(:private_synapse) { create :synapse, user: owner, permission: :private }
let(:public_synapse) { create :synapse, user: owner, permission: :public }
it 'prevents deletion by non-owner' do
expect(synapse.authorize_to_delete(other_user)).to eq false
expect(synapse.authorize_to_delete(owner)).to eq synapse
end
it 'prevents visibility if private' do
expect(synapse.authorize_to_show(other_user)).to eq synapse
expect(synapse.authorize_to_show(owner)).to eq synapse
expect(private_synapse.authorize_to_show(owner)).to eq private_synapse
expect(private_synapse.authorize_to_show(other_user)).to eq false
end
it 'only allows editing if commons or owned' do
expect(synapse.authorize_to_edit(other_user)).to eq synapse
expect(synapse.authorize_to_edit(owner)).to eq synapse
expect(private_synapse.authorize_to_edit(other_user)).to eq false
expect(private_synapse.authorize_to_edit(owner)).to eq private_synapse
expect(public_synapse.authorize_to_edit(other_user)).to eq false
expect(public_synapse.authorize_to_edit(owner)).to eq public_synapse
end
end
end

81
spec/models/topic_spec.rb Normal file
View file

@ -0,0 +1,81 @@
require 'rails_helper'
RSpec.describe Topic, type: :model do
it { is_expected.to belong_to :user }
it { is_expected.to belong_to :metacode }
it { is_expected.to have_many :maps }
it { is_expected.to have_many :mappings }
it { is_expected.to validate_presence_of :permission }
it { is_expected.to validate_inclusion_of(:permission).in_array Perm::ISSIONS.map(&:to_s) }
context 'has_viewable_synapses function' do
let (:user) { create(:user) }
let (:other_user) { create(:user) }
context 'topic with no synapses' do
let (:topic) { create(:topic) }
it 'returns false' do
expect(topic.has_viewable_synapses(user)).to eq false
end
end
context 'topic with one unpermitted synapse' do
let (:synapse) { create(:synapse, permission: :private, user: other_user) }
let (:topic) { create(:topic, synapses1: [synapse]) }
it 'returns false' do
expect(topic.has_viewable_synapses(user)).to eq false
end
end
context 'topic with one permitted synapse' do
let (:synapse) { create(:synapse, permission: :private, user: user) }
let(:topic) { create(:topic, synapses1: [synapse]) }
it 'returns true' do
expect(topic.has_viewable_synapses(user)).to eq true
end
end
context 'topic with one unpermitted, one permitted synapse' do
let (:synapse1) { create(:synapse, permission: :private, user: other_user) }
let (:synapse2) { create(:synapse, permission: :private, user: user) }
let (:topic) { create(:topic, synapses1: [synapse1, synapse2]) }
it 'returns true' do
expect(topic.synapses.count).to eq 2
expect(topic.has_viewable_synapses(user)).to eq true
end
end
end
context 'permssions' do
let(:owner) { create :user }
let(:other_user) { create :user }
let(:topic) { create :topic, user: owner, permission: :commons }
let(:private_topic) { create :topic, user: owner, permission: :private }
let(:public_topic) { create :topic, user: owner, permission: :public }
it 'prevents deletion by non-owner' do
expect(topic.authorize_to_delete(other_user)).to eq false
expect(topic.authorize_to_delete(owner)).to eq topic
end
it 'prevents visibility if private' do
expect(topic.authorize_to_show(other_user)).to eq topic
expect(topic.authorize_to_show(owner)).to eq topic
expect(private_topic.authorize_to_show(owner)).to eq private_topic
expect(private_topic.authorize_to_show(other_user)).to eq false
end
it 'only allows editing if commons or owned' do
expect(topic.authorize_to_edit(other_user)).to eq topic
expect(topic.authorize_to_edit(owner)).to eq topic
expect(private_topic.authorize_to_edit(other_user)).to eq false
expect(private_topic.authorize_to_edit(owner)).to eq private_topic
expect(public_topic.authorize_to_edit(other_user)).to eq false
expect(public_topic.authorize_to_edit(owner)).to eq public_topic
end
end
end

45
spec/rails_helper.rb Normal file
View file

@ -0,0 +1,45 @@
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
if Rails.env.production?
abort('The Rails environment is running in production mode!')
end
require 'spec_helper'
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!
# require all support files
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord 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
# 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.include Devise::TestHelpers, type: :controller
config.include ControllerHelpers, type: :controller
config.include Shoulda::Matchers::ActiveModel, type: :model
config.include Shoulda::Matchers::ActiveRecord, type: :model
end

9
spec/spec_helper.rb Normal file
View file

@ -0,0 +1,9 @@
RSpec.configure do |config|
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end

View file

@ -0,0 +1,17 @@
# https://github.com/plataformatec/devise/wiki/How-To:-Stub-authentication-in-controller-specs
module ControllerHelpers
# rubocop:disable Metrics/AbcSize
def sign_in(user = create(:user))
if user.nil? # simulate unauthenticated
allow(request.env['warden']).to(
receive(:authenticate!).and_throw(:warden, scope: :user)
)
else # simulate authenticated
allow(request.env['warden']).to(
receive(:authenticate!).and_return(user)
)
end
allow(controller).to receive(:current_user).and_return(user)
end
# rubocop:enable Metrics/AbcSize
end

View file

@ -0,0 +1,4 @@
# lets you type create(:user) instead of FactoryGirl.create(:user)
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end

View file

View file

@ -1,9 +0,0 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
#one:
# metacode:
# metacode_set:
#two:
# metacode:
# metacode_set:

View file

@ -1,11 +0,0 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
# This model initially had no columns defined. If you add columns to the
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
#one: {}
# column: value
#
#two: {}
# column: value

View file

@ -1,13 +0,0 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
#one:
# name: MyString
# desc: MyText
# user:
# mapperContributed: false
#two:
# name: MyString
# desc: MyText
# user:
# mapperContributed: false

View file

@ -1,240 +0,0 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
# This model initially had no columns defined. If you add columns to the
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one:
name: Action
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_action.png
color: #BD6C85
two:
name: Activity
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_activity.png
color: #6EBF65
three:
name: Catalyst
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_catalyst.png
color: #EF8964
four:
name: Closed
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_closedissue.png
color: #ABB49F
five:
name: Process
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_process.png
color: #BDB25E
six:
name: Future Dev
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_futuredev.png
color: #25A17F
seven:
name: Group
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_group.png
color: #7076BC
eight:
name: Implication
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_implication.png
color: #83DECA
nine:
name: Insight
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_insight.png
color: #B074AD
ten:
name: Intention
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_intention.png
color: #BAEAFF
eleven:
name: Knowledge
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_knowledge.png
color: #60ACF7
twelve:
name: Location
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_location.png
color: #ABD9A7
thirteen:
name: Need
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_need.png
color: #D2A7D4
fourteen:
name: Open Issue
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_openissue.png
color: #9BBF71
fifteen:
name: Opportunity
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_opportunity.png
color: #889F64
sixteen:
name: Person
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_person.png
color: #DE925F
seventeen:
name: Platform
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_platform.png
color: #21C8FE
eighteen:
name: Problem
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_problem.png
color: #99CFC4
nineteen:
name: Resource
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_resource.png
color: #C98C63
twenty:
name: Role
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_role.png
color: #A8595D
twenty-one:
name: Task
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_task.png
color: #3397C4
twenty-two:
name: Trajectory
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/blueprint/96px/bp_trajectory.png
color: #D3AA4C
twenty-three:
name: Argument
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_argument.png
color: #7FAEFD
twenty-four:
name: Con
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_con.png
color: #CF7C74
twenty-five:
name: Subject
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_subject.png
color: #8293D8
twenty-six:
name: Decision
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_decision.png
color: #CCA866
twenty-seven:
name: Event
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_event.png
color: #F5854B
twenty-eight:
name: Example
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_example.png
color: #618C61
twenty-nine:
name: Experience
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_experience.png
color: #BE995F
thirty:
name: Feedback
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_feedback.png
color: #54A19D
thirty-one:
name: Aim
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_aim.png
color: #B0B0B0
thirty-two:
name: Good Practice
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_goodpractice.png
color: #BD9E86
thirty-three:
name: Idea
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_idea.png
color: #C4BC5E
thirty-four:
name: List
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_list.png
color: #B7A499
thirty-five:
name: Media
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_media.png
color: #6D94CC
thirty-six:
name: Metamap
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_metamap.png
color: #AEA9FD
thirty-seven:
name: Model
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_model.png
color: #B385BA
thirty-eight:
name: Note
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_note.png
color: #A389A1
thirty-nine:
name: Perspective
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_perspective.png
color: #2EB6CC
forty:
name: Pro
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_pro.png
color: #89B879
forty-one:
name: Project
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_project.png
color: #85A050
forty-two:
name: Question
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_question.png
color: #5CB3B3
forty-three:
name: Reference
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_reference.png
color: #A7A7A7
forty-four:
name: Research
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_research.png
color: #CD8E89
forty-five:
name: Status update
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_status.png
color: #EFA7C0
forty-six:
name: Tool
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_tool.png
color: #828282
forty-seven:
name: Wildcard
icon: https://s3.amazonaws.com/metamaps-assets/metacodes/generics/96px/gen_wildcard.png
color: #73C7DE

View file

@ -1,11 +0,0 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
# This model initially had no columns defined. If you add columns to the
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
#one: {}
# column: value
#
#two: {}
# column: value

View file

@ -1,17 +0,0 @@
#password: toolsplusconsciousness
user:
name: user
email: user@user.com
encrypted_password: $2a$10$psR68SWYNy5ZKQPs9FrFM.HuRMrTXO/YFzv.HaUmdCsQZsQrG1XAW
code: qwertyui
joinedwithcode: qwertyui
admin: false
admin:
name: admin
email: admin@admin.com
encrypted_password: $2a$10$psR68SWYNy5ZKQPs9FrFM.HuRMrTXO/YFzv.HaUmdCsQZsQrG1XAW
code: iuytrewq
joinedwithcode: iuytrewq
admin: true

View file

@ -1,49 +0,0 @@
require 'test_helper'
class InMetacodeSetsControllerTest < ActionController::TestCase
setup do
@in_metacode_set = in_metacode_sets(:one)
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:in_metacode_sets)
end
test "should get new" do
get :new
assert_response :success
end
test "should create in_metacode_set" do
assert_difference('InMetacodeSet.count') do
post :create, in_metacode_set: { }
end
assert_redirected_to in_metacode_set_path(assigns(:in_metacode_set))
end
test "should show in_metacode_set" do
get :show, id: @in_metacode_set
assert_response :success
end
test "should get edit" do
get :edit, id: @in_metacode_set
assert_response :success
end
test "should update in_metacode_set" do
put :update, id: @in_metacode_set, in_metacode_set: { }
assert_redirected_to in_metacode_set_path(assigns(:in_metacode_set))
end
test "should destroy in_metacode_set" do
assert_difference('InMetacodeSet.count', -1) do
delete :destroy, id: @in_metacode_set
end
assert_redirected_to in_metacode_sets_path
end
end

View file

@ -1,7 +0,0 @@
require 'test_helper'
class MainControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end

View file

@ -1,9 +0,0 @@
require 'test_helper'
class MappingControllerTest < ActionController::TestCase
test "should get new" do
get :new
assert_response :success
end
end

View file

@ -1,7 +0,0 @@
require 'test_helper'
class MapsControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end

View file

@ -1,49 +0,0 @@
require 'test_helper'
class MetacodeSetsControllerTest < ActionController::TestCase
setup do
@metacode_set = metacode_sets(:one)
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:metacode_sets)
end
test "should get new" do
get :new
assert_response :success
end
test "should create metacode_set" do
assert_difference('MetacodeSet.count') do
post :create, metacode_set: { desc: @metacode_set.desc, mapperContributed: @metacode_set.mapperContributed, name: @metacode_set.name }
end
assert_redirected_to metacode_set_path(assigns(:metacode_set))
end
test "should show metacode_set" do
get :show, id: @metacode_set
assert_response :success
end
test "should get edit" do
get :edit, id: @metacode_set
assert_response :success
end
test "should update metacode_set" do
put :update, id: @metacode_set, metacode_set: { desc: @metacode_set.desc, mapperContributed: @metacode_set.mapperContributed, name: @metacode_set.name }
assert_redirected_to metacode_set_path(assigns(:metacode_set))
end
test "should destroy metacode_set" do
assert_difference('MetacodeSet.count', -1) do
delete :destroy, id: @metacode_set
end
assert_redirected_to metacode_sets_path
end
end

View file

@ -1,49 +0,0 @@
require 'test_helper'
class MetacodesControllerTest < ActionController::TestCase
setup do
@metacode = metacodes(:one)
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:metacodes)
end
test "should get new" do
get :new
assert_response :success
end
test "should create metacode" do
assert_difference('Metacode.count') do
post :create, metacode: { icon: @metacode.icon, name: @metacode.name }
end
assert_redirected_to metacode_path(assigns(:metacode))
end
test "should show metacode" do
get :show, id: @metacode
assert_response :success
end
test "should get edit" do
get :edit, id: @metacode
assert_response :success
end
test "should update metacode" do
put :update, id: @metacode, metacode: { icon: @metacode.icon, name: @metacode.name }
assert_redirected_to metacode_path(assigns(:metacode))
end
test "should destroy metacode" do
assert_difference('Metacode.count', -1) do
delete :destroy, id: @metacode
end
assert_redirected_to metacodes_path
end
end

View file

@ -1,7 +0,0 @@
require 'test_helper'
class SessionsControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end

View file

@ -1,7 +0,0 @@
require 'test_helper'
class SynapsesControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end

View file

@ -1,7 +0,0 @@
require 'test_helper'
class TopicsControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end

View file

@ -1,7 +0,0 @@
require 'test_helper'
class UsersControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end

View file

@ -1,12 +0,0 @@
require 'test_helper'
require 'rails/performance_test_help'
class BrowsingTest < ActionDispatch::PerformanceTest
# Refer to the documentation for all available options
# self.profile_options = { :runs => 5, :metrics => [:wall_time, :memory]
# :output => 'tmp/performance', :formats => [:flat] }
def test_homepage
get '/'
end
end

View file

@ -1,13 +0,0 @@
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
#
# Note: You'll currently still have to declare fixtures explicitly in integration tests
# -- they do not yet inherit this setting
fixtures :all
# Add more helper methods to be used by all tests here...
end

View file

View file

@ -1,4 +0,0 @@
require 'test_helper'
class InMetacodeSetsHelperTest < ActionView::TestCase
end

View file

@ -1,4 +0,0 @@
require 'test_helper'
class MainHelperTest < ActionView::TestCase
end

View file

@ -1,4 +0,0 @@
require 'test_helper'
class MappingHelperTest < ActionView::TestCase
end

View file

@ -1,4 +0,0 @@
require 'test_helper'
class MapsHelperTest < ActionView::TestCase
end

View file

@ -1,4 +0,0 @@
require 'test_helper'
class MetacodeSetsHelperTest < ActionView::TestCase
end

View file

@ -1,4 +0,0 @@
require 'test_helper'
class MetacodesHelperTest < ActionView::TestCase
end

View file

@ -1,4 +0,0 @@
require 'test_helper'
class SessionsHelperTest < ActionView::TestCase
end

View file

@ -1,4 +0,0 @@
require 'test_helper'
class SynapsesHelperTest < ActionView::TestCase
end

View file

@ -1,4 +0,0 @@
require 'test_helper'
class TopicsHelperTest < ActionView::TestCase
end

View file

@ -1,4 +0,0 @@
require 'test_helper'
class UsersHelperTest < ActionView::TestCase
end

View file

@ -1,7 +0,0 @@
require 'test_helper'
class InMetacodeSetTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View file

@ -1,7 +0,0 @@
require 'test_helper'
class MapTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View file

@ -1,7 +0,0 @@
require 'test_helper'
class MappingTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View file

@ -1,7 +0,0 @@
require 'test_helper'
class MetacodeSetTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View file

@ -1,7 +0,0 @@
require 'test_helper'
class MetacodeTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View file

@ -1,7 +0,0 @@
require 'test_helper'
class SessionTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View file

@ -1,7 +0,0 @@
require 'test_helper'
class SynapseTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View file

@ -1,7 +0,0 @@
require 'test_helper'
class TopicTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View file

@ -1,7 +0,0 @@
require 'test_helper'
class UserTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end