add rspec tests

This commit is contained in:
Josh 2018-05-18 11:51:03 -04:00
parent 6cdcd89387
commit f189c98c56
12 changed files with 451 additions and 6 deletions

1
.rspec Normal file
View File

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

View File

@ -54,6 +54,12 @@ group :development, :test do
# Environment configuration.
gem 'dotenv-rails'
# Testing.
gem 'rspec-rails', '~> 3.7'
gem 'rails-controller-testing'
gem 'faker'
gem "factory_bot_rails"
end
group :development do

View File

@ -58,12 +58,20 @@ GEM
coffee-script-source (1.12.2)
concurrent-ruby (1.0.5)
crass (1.0.4)
diff-lcs (1.3)
dotenv (2.2.1)
dotenv-rails (2.2.1)
dotenv (= 2.2.1)
railties (>= 3.2, < 5.2)
erubis (2.7.0)
execjs (2.7.0)
factory_bot (4.8.2)
activesupport (>= 3.0.0)
factory_bot_rails (4.8.2)
factory_bot (~> 4.8.2)
railties (>= 3.0.0)
faker (1.8.7)
i18n (>= 0.7)
faraday (0.12.2)
multipart-post (>= 1.2, < 3)
ffi (1.9.23)
@ -139,6 +147,10 @@ GEM
bundler (>= 1.3.0)
railties (= 5.0.7)
sprockets-rails (>= 2.0.0)
rails-controller-testing (1.0.2)
actionpack (~> 5.x, >= 5.0.1)
actionview (~> 5.x, >= 5.0.1)
activesupport (~> 5.x)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
@ -154,6 +166,23 @@ GEM
rb-fsevent (0.10.3)
rb-inotify (0.9.10)
ffi (>= 0.5.0, < 2)
rspec-core (3.7.1)
rspec-support (~> 3.7.0)
rspec-expectations (3.7.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.7.0)
rspec-mocks (3.7.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.7.0)
rspec-rails (3.7.2)
actionpack (>= 3.0)
activesupport (>= 3.0)
railties (>= 3.0)
rspec-core (~> 3.7.0)
rspec-expectations (~> 3.7.0)
rspec-mocks (~> 3.7.0)
rspec-support (~> 3.7.0)
rspec-support (3.7.1)
sass (3.5.6)
sass-listen (~> 4.0.0)
sass-listen (4.0.0)
@ -209,6 +238,8 @@ DEPENDENCIES
byebug
coffee-rails (~> 4.2)
dotenv-rails
factory_bot_rails
faker
font-awesome-sass (= 4.7.0)
jbuilder (~> 2.5)
jquery-rails
@ -218,6 +249,8 @@ DEPENDENCIES
omniauth-twitter
puma (~> 3.0)
rails (~> 5.0.7)
rails-controller-testing
rspec-rails (~> 3.7)
sass-rails (~> 5.0)
spring
spring-watcher-listen (~> 2.0.0)

View File

@ -9,7 +9,8 @@ class Meeting < ApplicationRecord
RETURNCODE_SUCCESS = "SUCCESS"
# Creates a meeting on the BigBlueButton server.
def create(options = {})
def start_meeting(options = {})
puts bbb
create_options = {
record: options[:meeting_recorded].to_s,
logoutURL: options[:meeting_logout_url] || '',
@ -34,7 +35,7 @@ class Meeting < ApplicationRecord
# Returns a URL to join a user into a meeting.
def join_path(username, options = {})
# Create the meeting if it isn't running.
create(options) unless is_running?
start_meeting(options) unless is_running?
# Set meeting options.
options[:meeting_logout_url] ||= nil
@ -134,11 +135,11 @@ class Meeting < ApplicationRecord
encoded_params = OAuth::Helper.normalize(params)
string = "getUser" + encoded_params + secret
checksum = OpenSSL::Digest.digest('sha1', string).unpack("H*").first
URI.parse("#{base_url}?#{encoded_params}&checksum=#{checksum}")
end
# Removes trailing forward slash from BigBlueButton URL.
# Removes trailing forward slash from a URL.
def remove_slash(s)
s.nil? ? nil : s.chomp("/")
end

View File

@ -0,0 +1,74 @@
require 'rails_helper'
describe SessionsController, type: :controller do
context "GET #new," do
it "renders the #new view." do
get :new
expect(response).to render_template :new
end
end
context "POST #create," do
it "should signin user." do
user = create(:user)
post :create, params: {session: {email: user.email, password: user.password}}
expect(response).to redirect_to room_path(user.room.uid)
expect(user.id).to eql(session[:user_id])
end
it "should render new on fail." do
user = create(:user)
post :create, params: {session: {email: user.email, password: "incorrect_password"}}
expect(response).to render_template :new
end
end
context "POST #launch," do
it "should login launched user." do
end
end
context "POST #omniauth," do
it "should login omniauth user." do
email = "omniauth@test.com"
uid = "123456789"
OmniAuth.config.test_mode = true
OmniAuth.config.add_mock(:twitter, {
provider: "twitter",
uid: uid,
info: {
email: email,
name: "Omni User",
nickname: "username"
}
})
get "/auth/twitter"
request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:twitter]
get omniauth_session_path(provider: "twitter")
user = User.find_by(email: email, uid: uid)
expect(response).to redirect_to room_path(user.room.uid)
expect(user.id).to eql(session[:user_id])
end
end
context "GET #destroy," do
it "should logout user." do
user = create(:user)
session[:user_id] = user.id
get :destroy
expect(response).to redirect_to root_path
expect(user.id).to_not eql(session[:user_id])
end
end
end

25
spec/factories.rb Normal file
View File

@ -0,0 +1,25 @@
FactoryBot.define do
factory :user do
password = Faker::Internet.password(8)
provider { %w(greenlight google twitter).sample }
uid { rand(10 ** 8) }
name { Faker::Name.first_name }
username { Faker::Internet.user_name }
email { Faker::Internet.email }
password { password }
password_confirmation { password }
end
factory :room do
uid { rand(10 ** 8) }
user { create(:user) }
end
factory :meeting do
uid { rand(10 ** 8) }
name { Faker::Pokemon.name }
room { create(:room) }
end
end

View File

@ -0,0 +1,19 @@
require "rails_helper"
describe Meeting, type: :model do
it "should be valid." do
meeting = create(:meeting)
expect(meeting).to be_valid
end
it "name should be present." do
meeting = build(:meeting, name: nil)
expect(meeting).to_not be_valid
end
it "#random_password is random." do
meeting = create(:meeting)
expect(meeting.send(:random_password, 10)).to_not eql(meeting.send(:random_password, 10))
end
end

21
spec/models/room_spec.rb Normal file
View File

@ -0,0 +1,21 @@
require "rails_helper"
describe Room, type: :model do
describe "#owned_by?" do
it "should identify correct owner." do
room = create(:room)
expect(room.owned_by?(room.user)).to eql(true)
end
it "should identify incorrect owner." do
room = create(:room)
expect(room.owned_by?(create(:user))).to eql(false)
end
it "should return false when user is nil." do
room = create(:room)
expect(room.owned_by?(nil)).to eql(false)
end
end
end

105
spec/models/user_spec.rb Normal file
View File

@ -0,0 +1,105 @@
require "rails_helper"
describe User, type: :model do
it "should be valid." do
user = create(:user)
expect(user).to be_valid
end
it "name should be present." do
user = build(:user, name: nil)
expect(user).to_not be_valid
end
it "username should be present." do
user = build(:user, username: nil)
expect(user).to_not be_valid
end
it "provider should be present." do
user = build(:user, provider: nil)
expect(user).to_not be_valid
end
it "should allow nil email." do
user = build(:user, email: nil)
expect(user).to be_valid
end
it "should allow nil uid." do
user = build(:user, uid: nil)
expect(user).to be_valid
end
it "should allow nil password." do
user = build(:user, password: nil, password_confirmation: nil)
expect(user).to be_valid
end
it "password should be longer than six characters." do
user = build(:user, password: "short")
expect(user).to_not be_valid
end
it "should create user from omniauth." do
auth = {
"uid" => "123456789",
"provider" => "twitter",
"info" => {
"name" => "Test Name",
"nickname" => "username",
"email" => "test@example.com"
}
}
user = User.from_omniauth(auth)
expect(user.name).to eql(auth["info"]["name"])
expect(user.username).to eql(auth["info"]["nickname"])
end
it "email addresses should be saved as lower-case." do
mixed = "ExAmPlE@eXaMpLe.CoM"
user = build(:user, email: mixed)
user.save
expect(user.email).to eql(mixed.downcase)
end
it "email validation should reject invalid addresses." do
invalid_addresses = %w[user@example,com user_at_foo.org user.name@example. foo@bar_baz.com foo@bar+baz.com]
user = create(:user)
invalid_addresses.each do |invalid_address|
user.email = invalid_address
expect(user).to_not be_valid
end
end
it "email should be unique." do
user = create(:user)
duplicate = user.dup
expect(duplicate).to_not be_valid
end
it "name should not be too long." do
user = build(:user, name: "a" * 25)
expect(user).to_not be_valid
end
it "email should not be too long." do
user = build(:user, email: "a" * 50 + "@example.com")
expect(user).to_not be_valid
end
it "should generate room and meeting when saved." do
user = create(:user)
user.save
expect(user.room).to be_instance_of Room
expect(user.room.meeting).to be_instance_of Meeting
end
end

58
spec/rails_helper.rb Normal file
View File

@ -0,0 +1,58 @@
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!
# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
#
# The following line is provided for convenience purposes. It has the downside
# of increasing the boot-up time by auto-requiring all files in the support
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
# Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
# Checks for pending migrations and applies them before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!
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!
# Filter lines from Rails gems in backtraces.
config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")
end

102
spec/spec_helper.rb Normal file
View File

@ -0,0 +1,102 @@
require 'faker'
require 'factory_bot_rails'
# This file was generated by the `rails generate rspec:install` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause
# this file to always be loaded, without a need to explicitly require it in any
# files.
#
# Given that it is always loaded, you are encouraged to keep this file as
# light-weight as possible. Requiring heavyweight dependencies from this file
# will add to the boot time of your test suite on EVERY test run, even for an
# individual file that may not need all of that loaded. Instead, consider making
# a separate helper file that requires the additional dependencies and performs
# the additional setup, and require it from the spec files that actually need
# it.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
# assertions if you prefer.
config.expect_with :rspec do |expectations|
# This option will default to `true` in RSpec 4. It makes the `description`
# and `failure_message` of custom matchers include text for helper methods
# defined using `chain`, e.g.:
# be_bigger_than(2).and_smaller_than(4).description
# # => "be bigger than 2 and smaller than 4"
# ...rather than:
# # => "be bigger than 2"
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
# rspec-mocks config goes here. You can use an alternate test double
# library (such as bogus or mocha) by changing the `mock_with` option here.
config.mock_with :rspec do |mocks|
# Prevents you from mocking or stubbing a method that does not exist on
# a real object. This is generally recommended, and will default to
# `true` in RSpec 4.
mocks.verify_partial_doubles = true
end
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
# have no way to turn it off -- the option exists only for backwards
# compatibility in RSpec 3). It causes shared context metadata to be
# inherited by the metadata hash of host groups and examples, rather than
# triggering implicit auto-inclusion in groups with matching metadata.
config.shared_context_metadata_behavior = :apply_to_host_groups
# Include FactoryGirl.
config.include FactoryBot::Syntax::Methods
# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.
=begin
# This allows you to limit a spec run to individual examples or groups
# you care about by tagging them with `:focus` metadata. When nothing
# is tagged with `:focus`, all examples get run. RSpec also provides
# aliases for `it`, `describe`, and `context` that include `:focus`
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
config.filter_run_when_matching :focus
# Allows RSpec to persist some state between runs in order to support
# the `--only-failures` and `--next-failure` CLI options. We recommend
# you configure your source control system to ignore this file.
config.example_status_persistence_file_path = "spec/examples.txt"
# Limits the available syntax to the non-monkey patched syntax that is
# recommended. For more details, see:
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
config.disable_monkey_patching!
# Many RSpec users commonly either run the entire suite or an individual
# file, and it's useful to allow more verbose output when running an
# individual spec file.
if config.files_to_run.one?
# Use the documentation formatter for detailed output,
# unless a formatter has already been configured
# (e.g. via a command-line flag).
config.default_formatter = "doc"
end
# Print the 10 slowest examples and example groups at the
# end of the spec run, to help surface which specs are running
# particularly slow.
config.profile_examples = 10
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = :random
# Seed global randomization in this process using the `--seed` CLI option.
# Setting this allows you to use `--seed` to deterministically reproduce
# test failures related to randomization by passing the same `--seed` value
# as the one that triggered the failure.
Kernel.srand config.seed
=end
end

View File

@ -30,10 +30,10 @@ class SessionsControllerTest < ActionDispatch::IntegrationTest
}
})
get "/auth/twitter"
#get "/auth/twitter"
request.env['omniauth.auth'] = OmniAuth.config.mock_auth[:twitter]
get omniauth_session_path(provider: "twitter")
#get omniauth_session_path(provider: "twitter")
user = User.find_by(email: email, uid: uid)