start writing tests

This commit is contained in:
Josh
2018-05-09 16:31:52 -04:00
parent e6d01ef1b9
commit 5347d902c0
24 changed files with 370 additions and 192 deletions

View File

@ -86,7 +86,8 @@ class MeetingsController < ApplicationController
private
def meeting_params(room)
params.require(:meeting).permit(:name).merge!(room_id: room.id)
params.require(:meeting).permit(:name).merge!(room: room)
#params.require(:meeting).permit(:name).merge!(room_id: room.id)
end
def default_meeting_options

View File

@ -6,17 +6,35 @@ class SessionsController < ApplicationController
# GET /logout
def destroy
logout
logout if current_user
end
# POST /login
def create
user = User.find_by(email: session_params[:email])
if user && user.authenticate(session_params[:password])
login(user)
else
# Login unsuccessful, display error message.
render :new
end
end
# GET/POST /auth/:provider/callback
def create
def omniauth_session
user = User.from_omniauth(request.env['omniauth.auth'])
login(user)
end
# POST /auth/failure
def fail
redirect_to root_path
end
private
def session_params
params.require(:session).permit(:email, :password)
end
end

View File

@ -1,2 +1,23 @@
class UsersController < ApplicationController
# GET /signup
def new
@user = User.new
end
# POST /signup
def create
user = User.new(user_params)
if user.save
login(user)
else
render :new
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
end

View File

@ -1,149 +0,0 @@
module BigBlueHelper
META_LISTED = "gl-listed"
META_TOKEN = "gl-token"
def bbb_endpoint
Rails.configuration.bigbluebutton_endpoint
end
def bbb_secret
Rails.configuration.bigbluebutton_secret
end
def bbb
@bbb ||= BigBlueButton::BigBlueButtonApi.new(bbb_endpoint + "api", bbb_secret, "0.8")
end
# Generates a BigBlueButton meeting id from a meeting token.
def bbb_meeting_id(id)
Digest::SHA1.hexdigest(Rails.application.secrets[:secret_key_base] + id).to_s
end
# Generates a random password for a meeting.
def random_password(length)
o = ([('a'..'z'), ('A'..'Z')].map do |i| i.to_a end).flatten
((0...length).map do o[rand(o.length)] end).join
end
# Checks if a meeting is running on the BigBlueButton server.
def meeting_is_running?(id)
begin
bbb.get_meeting_info(id, nil)
return true
rescue BigBlueButton::BigBlueButtonException => exc
return false
end
end
def start_meeting(options)
meeting_id = bbb_meeting_id(name)
# Need to create the meeting on the BigBlueButton server.
create_options = {
record: options[:meeting_recorded].to_s,
#logoutURL: options[:meeting_logout_url] || request.base_url,
moderatorPW: random_password(12),
attendeePW: random_password(12),
moderatorOnlyMessage: options[:moderator_message],
"meta_#{BigBlueHelper::META_LISTED}": false,
"meta_#{BigBlueHelper::META_TOKEN}": name
}
#meeting_options.merge!(
#{ "meta_room-id": options[:room_owner],
# "meta_meeting-name": options[:meeting_name]}
#) if options[:room_owner]
# Send the create request.
begin
bbb.create_meeting(name, meeting_id, create_options)
rescue BigBlueButton::BigBlueButtonException => exc
puts "BigBlueButton failed on create: #{exc.key}: #{exc.message}"
end
# Get the meeting info.
#bbb_meeting_info = bbb.get_meeting_info(meeting_id, nil)
meeting_id
end
# Generates a URL to join a BigBlueButton session.
def join_url(meeting_id, username, options = {})
options[:meeting_recorded] ||= false
options[:user_is_moderator] ||= false
options[:wait_for_moderator] ||= false
options[:meeting_logout_url] ||= nil
options[:meeting_name] ||= name
options[:room_owner] ||= nil
options[:moderator_message] ||= ''
return call_invalid_res if !bbb
# Get the meeting info.
meeting_info = bbb.get_meeting_info(meeting_id, nil)
# Determine the password to use when joining.
password = if options[:user_is_moderator]
meeting_info[:moderatorPW]
else
meeting_info[:attendeePW]
end
# Generate the join URL.
bbb.join_meeting_url(meeting_id, username, password)
end
# Generates a URL to join a BigBlueButton session.
def join_url_old(meeting_token, full_name, options={})
options[:meeting_recorded] ||= false
options[:user_is_moderator] ||= false
options[:wait_for_moderator] ||= false
options[:meeting_logout_url] ||= nil
options[:meeting_name] ||= meeting_token
options[:room_owner] ||= nil
options[:moderator_message] ||= ''
return call_invalid_res if !bbb
meeting_id = bbb_meeting_id(meeting_token)
unless meeting_is_running?(meeting_id)
# Need to create the meeting on the BigBlueButton server.
create_options = {
record: options[:meeting_recorded].to_s,
logoutURL: options[:meeting_logout_url] || request.base_url,
moderatorPW: random_password(12),
attendeePW: random_password(12),
moderatorOnlyMessage: options[:moderator_message],
"meta_#{BigBlueHelper::META_LISTED}": false,
"meta_#{BigBlueHelper::META_TOKEN}": meeting_token
}
#meeting_options.merge!(
#{ "meta_room-id": options[:room_owner],
# "meta_meeting-name": options[:meeting_name]}
#) if options[:room_owner]
# Send the create request.
begin
bbb.create_meeting(options[:meeting_name], meeting_id, create_options)
rescue BigBlueButton::BigBlueButtonException => exc
puts "BigBlueButton failed on create: #{exc.key}: #{exc.message}"
end
# Get the meeting info.
bbb_meeting_info = bbb.get_meeting_info(meeting_id, nil)
end
# Determine the password to use when joining.
password = if options[:user_is_moderator]
bbb_meeting_info[:moderatorPW]
else
bbb_meeting_info[:attendeePW]
end
# Generate the join URL.
bbb.join_meeting_url(meeting_id, full_name, password)
end
end

View File

@ -2,6 +2,8 @@ class Meeting < ApplicationRecord
before_create :generate_meeting_id
validates :name, presence: true
belongs_to :room
# Creates a meeting on the BigBlueButton server.
@ -11,9 +13,7 @@ class Meeting < ApplicationRecord
logoutURL: options[:meeting_logout_url] || '',
moderatorPW: random_password(12),
attendeePW: random_password(12),
moderatorOnlyMessage: options[:moderator_message],
"meta_#{BigBlueHelper::META_LISTED}": false,
"meta_#{BigBlueHelper::META_TOKEN}": name
moderatorOnlyMessage: options[:moderator_message]
}
#meeting_options.merge!(

View File

@ -11,7 +11,10 @@ class Room < ApplicationRecord
private
# Generates a uid for the room.
def set_uid
self.uid = Digest::SHA1.hexdigest(user.uid + user.provider + user.username)[0..12]
digest = user.id.to_s + user.provider + user.username
digest += user.uid unless user.uid.nil?
self.uid = Digest::SHA1.hexdigest(digest)[0..12]
end
end

View File

@ -1,7 +1,23 @@
class User < ApplicationRecord
after_create :initialize_room
before_save { email.downcase! }
has_one :room
validates :name, length: { maximum: 24 }, presence: true
validates :username, presence: true
validates :provider, presence: true
validates :email, length: { maximum: 60 }, presence: true,
uniqueness: { case_sensitive: false },
format: {with: /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i }
validates :password, length: { minimum: 6 }, allow_nil: true
# We don't want to run the validations because they require a user
# to have a password. Users who authenticate via omniauth won't.
has_secure_password(validations: false)
class << self
# Generates a user from omniauth.
@ -10,10 +26,6 @@ class User < ApplicationRecord
user.name = send("#{auth['provider']}_name", auth)
user.username = send("#{auth['provider']}_username", auth)
user.email = send("#{auth['provider']}_email", auth)
#user.token = auth['credentials']['token']
# Create a room for the user if they don't have one.
user.room = Room.create unless user.room
user.save!
user
@ -48,4 +60,11 @@ class User < ApplicationRecord
end
private
# Initializes a room for the user.
def initialize_room
self.room = Room.new
self.save!
end
end

View File

@ -19,4 +19,4 @@
<% end %>
<br>
<%= link_to 'Logout', user_logout_path %>
<%= link_to 'Logout', logout_path %>

View File

@ -24,6 +24,21 @@
<% end %>
<% end %>
<center><p>or...<br><br></p></center>
<%= form_for(:session, url: login_path) do |f| %>
<div class="input-field col s12">
<%= f.label :email, "Email Address" %>
<%= f.text_field :email %>
</div>
<div class="input-field col s12">
<%= f.label :password %>
<%= f.password_field :password %>
</div>
<br>
<%= f.submit "Login", class: "btn white-text light-green" %>
<%= link_to "Don't have an account? Sign up!", signup_path %>
<% end %>
</div>
<% end %>
</div>

View File

@ -6,12 +6,12 @@
<% else %>
<%= link_to(t('return_to_room'), room_path(current_user)) %>
<% end %>
<div><%= link_to t('logout'), user_logout_url %></div>
<div><%= link_to t('logout'), logout_url %></div>
</div>
<% else %>
<div class="text-center">
<span><%= t('login_description') %></span>
<div><%= link_to t('login'), user_login_url %></div>
<div><%= link_to t('login'), login_url %></div>
</div>
<% end %>
</span>

View File