All Files (87.05% covered at 1.37 hits/line)
26 files in total.
865 relevant lines.
753 lines covered and
112 lines missed
- 1
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
- 1
protect_from_forgery with: :exception
- 1
def authenticate
- 20
render status: :unauthorized, nothing: true unless session['user_id']
end
- 1
private
- 1
def current_user
- 2
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
- 1
helper_method :current_user
end
- 1
class MissionsController < ApplicationController
- 1
before_action :authenticate
- 1
skip_before_action :verify_authenticity_token, :only => [
:create,
:getlist,
:detail,
:update,
:commentPublish
]
#======================================>获取当前项目某状态的任务列表
- 1
def getlist
#Todo
- 1
pid = params[:project_id]
- 1
stu = params[:status_type]
#missions = []
- 1
details = []
- 1
missions = Mission.where("project_id = ? AND status = ?",pid,stu).all
- 1
missions.each do |mission|
#puts mission.inspect
- 1
details += [{
:id => mission.id,
:name => mission.name,
:content => mission.content
}]
end
# Mission.where(:project_id => pid).where(:status => stu) do |i|
# missions += [{
# :id => i.id,
# :name => i.name,
# :content => i.content
# }]
# end
- 1
render :json => {
:code => 0,
:data => missions
}
end
#======================================>获取任务详细信息
- 1
def detail
#Todo
- 1
username = []
- 1
comments_s = []
- 1
mission_id = params[:id]
- 1
mission = Mission.find_by(id: mission_id)
- 1
m_us = Missions_user.where("mission_id = ?",mission_id).all
#puts m_us.inspect
- 1
m_us.each do |m_u|
user_id = m_u.user_id
user = User.find_by(id: user_id)
#puts user.inspect
username << user.name
end
- 1
comments = Comment.where("mission_id = ?",mission_id).all
- 1
comments.each do |c|
- 2
uname = User.find_by(id: c.user_id).name
- 2
comments_s += [{
:nickname => uname,
:content => c.content,
:time => c.created_at
}]
end
#puts comments_s.inspect
- 1
render :json => {
:code => 0,
:data => {
:name => mission.name,
:content => mission.content,
:priority => mission.priority,
:status => mission.status,
:deadline => mission.deadline,
:users => username,
:comments => comments_s
}
}
end
#======================================>创建任务
- 1
def create
#Todo
- 2
params_mission = {name: params[:name],project_id: params[:project_id],content: params[:content],deadline: params[:deadline],priority: params[:priority],status: params[:status]}
- 2
mission = Mission.new(params_mission)
- 2
if mission.save
- 2
render :json => {:code => 0}
else
render json: mission.errors, status: :unprocessable_entity
end
- 2
userids = params[:users]
- 2
userids.each do |id|
- 2
user_enties = User.find_by(id: id)
#puts user_enties.inspect
- 2
params_mu = {mission_id: mission.id,user_id: user_enties.id}
missions_user = Missions_user.new(params_mu)
missions_user.save
content = "有新任务 <a>"+mission.name+"</a>"
params_note = {content: content,user_id: user_enties.id,category: 3,project_id: mission.project_id}
note = Note.new(params_note)
note.save
end
end
#======================================>修改任务信息
- 1
def update
#Todo
id = params[:id]
#now = Datetime.now
mission = Mission.find_by(id: id)
params_mission = {name: params[:name],content: params[:content],deadline: params[:deadline],priority: params[:priority]}
#puts params_mission
mission.update(params_mission)
mission.save
Missions_user.delete_all(["mission_id = ?",id])
userids = params[:users]
userids.each do |name|
user_enties = User.find_by(id: id)
params = {mission_id: id,user_id: user_enties.id}
missions_user = Missions_user.new(params)
missions_user.save
content = "<a>"+mission.name + "</a> 任务信息发生了变化"
params_note = {content: content,user_id: user_enties.id,category: 3,project_id: mission.project_id}
note = Note.new(params_note)
note.save
end
render :json => {:code => 0}
end
#======================================>当前用户发表评论
- 1
def commentPublish
#Todo
- 3
mission_id = params[:mission_id]
- 3
content = params[:content]
- 3
user_id = session[:user_id]
- 3
params = {content: content, mission_id: mission_id, user_id: user_id}
- 3
comment = Comment.new(params)
- 3
if comment.save
- 3
render :json => {:code => 0}
else
render json: mission.errors, status: :unprocessable_entity
end
end
end
- 1
class NotesController < ApplicationController
- 1
before_action :authenticate
#获取用户当前的通知列表
- 1
def getNotesList
#Todo
- 1
pid = params[:project_id]
- 1
uid = session[:user_id]
- 1
notes = Note.where("user_id = ? AND project_id = ? ",uid,pid).order("created_at desc").all
- 1
note = Hash.new do |h,k|
- 1
h[k] = []
end
- 1
notes.each do |n|
- 2
date = n.created_at.to_s[/([\d\-]*)\s(.*)/,1]
- 2
time = n.created_at.to_s[/(.*)\s([\d\:]*)/,2]
note[date] << {
:content => n.content,
:time => time,
:type => n.category
- 2
}
end
- 1
data = []
- 1
note.each do |k,v|
- 1
data += [{
:time => k,
:notes => v
}]
end
- 1
render :json => {
:code => 0,
:data => data
}
end
end
- 1
class ProjectsController < ApplicationController
- 1
before_action :authenticate
- 1
skip_before_action :verify_authenticity_token, :only => [
:index,
:create,
:addUsers,
:detail,
:update,
:deleteUsers
]
#=============================================>获取我的项目列表,user_id保存在session中.
- 1
def index
#Todo
uid = session[:user_id]
data=[]
Projects_user.where(:user_id => uid).each do |i|
pid=i.project_id
p=Project.find_by(id:pid)
mc = Mission.where(:project_id => pid).size
uc = Projects_user.where(:project_id => pid).size
sc = Share.where(:project_id => pid).size
data+=[{
:id => pid,
:name => p.name,
:content => p.content,
:mission_count => mc,
:users_count => uc,
:shares_count => sc
}]
end
render :json => {
:code => 0,
:data => data
}
end
#=============================================>创建项目
- 1
def create
- 2
uid = session[:user_id]
- 2
project = Project.new(project_params)
if project.save
Projects_user.create(project_id:project.id,user_id:uid)
render status: :created, nothing: true
else
render json: project.errors, status: :unprocessable_entity
end
end
#=============================================>项目添加用户
- 1
def addUsers
- 2
uid = User.find_by(email:params[:email]).id# 需要添加的用户
register = session[:user_id]# 当前用户
pid = params[:project_id]
# 检查该项目中是否已经有要添加的用户
pu_exist=Projects_user.find_by(project_id:pid,user_id:uid)
if pu_exist.nil?# 不存在
params_pu = {project_id: pid,user_id: uid}
pu = Projects_user.new(params_pu)
pu.save
content = "<a>"+User.find_by(id: register).name + "</a> 邀请您进入项目 <a>" + Project.find_by(id: pid).name+"</a>"
puts content
params_note = {user_id: uid,content: content,category: 2,project_id: pid}
note = Note.new(params_note)
note.save!
render :json => {:code => 0}
else# 存在
render json:current_user.errors, status: :unprocessable_entity
end
end
#=============================================>项目删除用户
- 1
def deleteUsers
uid = params[:user_id]# 需要添加的用户
register = session[:user_id]# 当前用户
pid = params[:project_id]
# 检查该项目中是否已经有要添加的用户
projects_user=Projects_user.find_by(project_id:pid,user_id:uid)
if projects_user.nil?# 不存在
render json:current_user.errors, status: :unprocessable_entity
end
# 存在
puts projects_user.user_id
puts projects_user.project_id
Projects_user.where(project_id:pid,user_id:uid).delete_all
content ="<a>"+ User.find_by(id: register).name + "</a> 把您从项目 <a>" + Project.find_by(id: pid).name+"</a> 中删除"
puts content
params_note = {user_id: uid,content: content,category: 2,project_id: pid}
note = Note.new(params_note)
note.save!
render :json => {:code => 0}
end
#=============================================>项目详情
- 1
def detail
#Todo
pid = params[:id]
project = Project.find_by(id: pid)
#uid = Projects_user.where(:project_id => pid)
users = []
projects_users = Projects_user.where("project_id = ?",pid).all
projects_users.each do |projects_user|
uid = projects_user.user_id
i = User.find_by(id: uid)
users += [{
:name => i.name,
:id => i.id
}]
end
#puts users.inspect
# Users.find(uid).each do |i|
# users += [{
# :nickname => i.name,
# :id => i.id
# }]
# end
render :json => {
:code => 0,
:data => {
:name => project.name,
:content => project.content,
:users => users
}
}
end
#=============================================>项目修改
- 1
def update
project = Project.find_by(:id => params[:project][:id])
if project.update(project_params)
render :json => {:code => 0}
else
render json:current_user.errors, status: :unprocessable_entity
end
end
- 1
private
- 1
def project_params
- 2
params.require(:project).permit(:name,:content,:id)
end
end
- 1
class SessionsController < ApplicationController
# 除登录之外,其余接口必须在登录状态下访问
- 1
before_action :authenticate, except: [ :create ]
- 1
skip_before_action :verify_authenticity_token, :only => [:create,:destroy,:show]
# 用户登录
- 1
def create
- 11
user = User.find_by(email: params[:email])
- 11
if user && user.authenticate(params[:password])
- 6
session[:user_id] = user.id
- 6
render status: :ok, text: 'ok'
else
- 5
render status: :unauthorized, text: 'account or password is not correct'
end
end
- 1
def destroy
- 1
session.delete :user_id
- 1
@current_user &&= nil
- 1
render status: :ok, nothing: true
end
- 1
def show
- 2
render 'show'
end
end
- 1
class SharesController < ApplicationController
- 1
before_action :authenticate
- 1
skip_before_action :verify_authenticity_token, :only => [
:create,
:getShareList
]
#当前用户创建分享
- 1
def create
- 2
uid = session[:user_id].to_i
- 2
share=Share.new(share_params)
share.user_id=uid
if share.save
content = "<a>"+User.find(uid).name + "</a> 分享了一些事"
pid=share.project_id
Projects_user.where(:project_id => pid).each do |i|
params_note = {user_id: i.user_id,content: content,category: 1,project_id: pid}
note = Note.new(params_note)
note.save!
end
render status: :created, nothing: true
else
render json: project.errors, status: :unprocessable_entity
end
end
#获取项目的分享列表
- 1
def getShareList
#Todo
pid = params[:project_id]
data = []
Share.where(:project_id => pid).order("created_at desc").each do |i|
puts i.content
name = User.find(i.user_id).name
data += [{
:name => name,
:time => i.created_at,
:content => i.content
}]
end
render :json => {
:code => 0,
:data => data
}
end
- 1
private
- 1
def share_params
- 2
params.require(:share).permit(:project_id,:content)
end
end
- 1
class UsersController < ApplicationController
- 1
before_action :authenticate, except: [ :emailExist, :usernameExist, :create ]
- 1
skip_before_action :verify_authenticity_token, :only => [
:emailExist,
:usernameExist,
:create,
:update
]
- 1
def emailExist
- 4
if checkExist?(:email, params[:email])
- 1
render :text => '1'
else
- 3
render :text => '0'
end
end
- 1
def usernameExist
- 4
if checkExist?(:name, params[:username])
- 1
render :text => 'exist'
else
- 3
render :text => 'not exist'
end
end
- 1
def create
- 7
@user = User.new(user_params)
- 6
if @user.save
- 2
render status: :created, nothing: true
else
- 4
render json: @user.errors, status: :unprocessable_entity
end
end
- 1
def update
- 4
@user = current_user
- 4
if @user.update(user_params)
- 2
render 'show'
else
- 2
render json:current_user.errors, status: :unprocessable_entity
end
end
- 1
private
- 1
def checkExist?(field_name, value)
- 8
User.exists?(field_name => value)
end
- 1
def user_params
- 11
params.require(:user).permit(:name,:password, :password_confirmation, :email, :phone)
end
end
- 1
class Comment < ActiveRecord::Base
- 1
belongs_to :mission
- 1
validates :content, presence: true
- 1
validates :mission_id, presence: true
- 1
validates :user_id, presence: true
- 1
validate :comment_has_atleast_one_character, on: :create
- 1
validate :user_id_exist, on: :create
- 1
private
- 1
def comment_has_atleast_one_character
- 8
tmp = content.strip
- 7
if tmp.length < 1
- 1
errors[:content] = 'content should has at least one character'
end
end
- 1
def user_id_exist
- 7
user = User.find_by(id: user_id)
- 7
if user == nil
- 1
errors[:user] = "user is not exist"
end
end
# mission 是否存在的验证将放在controller中进行
# def mission_id_should_be_exist
# @mission = Mission.find(:mission_id)
# if @mission == nil
# errors[:mission_id] = "mission is not exist"
# end
# end
end
- 1
class Mission < ActiveRecord:: Base
- 1
has_many :comments
- 1
validates :name, presence: true
- 1
validates :deadline, presence: true
- 1
validates :priority, presence: true
- 1
validates :status, presence: true
- 1
validates :content, presence: true
- 1
validates :project_id, presence: true
- 1
validate :priority_is_among_valid_values
- 1
validate :status_is_among_valid_values
- 1
validate :project_exists, on: :create
- 1
private
- 1
def priority_is_among_valid_values
- 21
if priority != 1 and priority != 2 and priority != 3
- 2
errors[:priority] = "priority is not among valid values"
end
end
- 1
def status_is_among_valid_values
- 21
if status != 1 and status != 2 and status != 3 and status != 4
- 8
errors[:status] = "status is not among valid values"
end
end
- 1
def project_exists
#puts project_id
- 18
project = Project.find_by(id: project_id)
#puts project.inspect
- 18
if project == nil
- 8
errors[:project_id] = "project_id is not exist"
end
end
end
- 1
class Missions_user <ActiveRecord:: Base
- 1
validates :mission_id, presence: true
- 1
validates :user_id, presence: true
- 1
validate :mission_exist, on: :create
- 1
validate :user_exist, on: :create
- 1
private
- 1
def mission_exist
- 4
mission = Mission.find_by(id: mission_id)
- 4
if mission == nil
- 2
errors[:mission] = "mission belong to Missions_user is not exist"
end
end
- 1
def user_exist
- 4
user = User.find_by(id: user_id)
- 4
if user == nil
- 2
errors[:user] = "user belong to Missions_user is not exist"
end
end
end
- 1
class Note < ActiveRecord:: Base
- 1
belongs_to :user
- 1
validates :content, presence: true
- 1
validates :user_id, presence: true
- 1
validates :project_id, presence: true
- 1
validates :category, presence: true
- 1
validate :note_has_atleast_one_character
- 1
validate :note_category_should_among_valid_values
- 1
validate :user_exist, on: :create
- 1
validate :project_exist, on: :create
- 1
private
- 1
def note_has_atleast_one_character
- 11
tmp = content.strip
- 11
if tmp.length < 1
- 3
errors[:content] = "note content should has at least one character"
end
end
- 1
def note_category_should_among_valid_values
- 11
if category != 1 and category != 2 and category !=3
- 2
errors[:category] = "note category is not among valid values"
end
end
- 1
def user_exist
- 10
user = User.find_by(id: user_id)
- 10
if user == nil
- 2
errors[:user] = "user belong to Note is not exist"
end
end
- 1
def project_exist
- 10
project = Project.find_by(id: project_id)
- 10
if project == nil
- 5
errors[:project] = "project should be exist"
end
end
end
- 1
class Project < ActiveRecord::Base
- 1
has_and_belongs_to_many :users
- 1
validate :require_at_least_on_user,on: :update # 项目中最少要有一个用户
- 1
validates :name, presence: true, length: { minimum: 1, maximum: 50 }, uniqueness: true
- 1
private
- 1
def require_at_least_on_user
- 10
if !users || users.size == 0 # 这里使用size,可以智能选择需不需要查询数据库
errors[:users] = 'at least one user in the project'
end
end
end
- 1
class Share < ActiveRecord::Base
- 1
validates :content, presence: true
- 1
validates :project_id, presence: true
- 1
validates :user_id, presence: true
- 1
validate :share_has_atleast_one_character
- 1
validate :user_exists,on: :create
- 1
validate :project_exists,on: :create
- 1
private
- 1
def share_has_atleast_one_character
- 8
tmp = content.strip
- 8
if tmp.length < 1
- 2
errors[:content] = "share content should has at least one character"
end
end
- 1
def user_exists
- 7
user = User.find_by(id: user_id)
- 7
if user == nil
- 2
errors[:user] = "user belong to Share is not exist"
end
end
- 1
def project_exists
- 7
project = Project.find_by(id: project_id)
- 7
if project == nil
- 2
errors[:project] = "user belong to Share is not exist"
end
end
end
- 1
class User < ActiveRecord::Base
- 1
has_and_belongs_to_many :projects
- 1
has_and_belongs_to_many :missions
- 1
has_many :notes
- 1
has_secure_password # 等价于验证password_confirm和password是否相等,并且验证password是否存在
- 1
validates :name, presence: true, length: { maximum: 30 }
- 1
validates :email, presence: true, uniqueness: true, length: { maximum: 50},
format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i }
- 1
validates :password, length: { minimum: 6 },on:create
- 1
validates :phone, presence: true, format: { with: /\A[0-9]{11,11}\Z/i },
multiline: false
end
- 1
require 'rails_helper'
- 1
RSpec.describe NotesController,type: :controller do
- 1
describe "GET #getNotesList" do
- 1
it "return correct json format of noteslist" do
- 1
register = create(:user)
- 1
user = User.find_by(email: register.email)
- 1
session[:user_id] = user.id
- 1
params = {name: "project mission update test",content: "update test"}
- 1
project = Project.new(params)
- 1
project.users << register
- 1
project.save
- 1
params_note = {content: "note content 1",user_id: user.id,category: 2,project_id: project.id}
- 1
note = Note.new(params_note)
- 1
note.save
- 1
params_note2 = {content: "note content 2",user_id: user.id,category: 1,project_id: project.id}
- 1
note2 = Note.new(params_note2)
- 1
note2.save
- 1
post :getNotesList,project_id: project.id
- 1
puts response.body
end
end
end
- 1
require 'rails_helper'
- 1
RSpec.describe ProjectsController, type: :controller do
- 1
describe "GET #index " do
- 1
it "return valid projects json" do
- 1
register = create(:user)
- 1
user = User.find_by(email: register.email)
- 1
session[:user_id] = user.id
- 1
post :create,content: "project content 1",name: "project1"
post :create,content: "project content 2", name: "project2"
get :index
puts response.body
end
end
- 1
describe "POST #create" do
- 1
it "can create a valid project" do
- 1
register = create(:user)
- 1
user = User.find_by(email: register.email)
#puts user.inspect
- 1
session[:user_id] = user.id
- 1
post :create,content: "project content 1",name: "project1"
#puts response.body
end
end
- 1
describe "POST #addUsers" do
- 1
it "can addUsers correctly" do
- 1
register = create(:user)
- 1
user = User.find_by(email: register.email)
- 1
session[:user_id] = user.id
#user1 = create(:user)
- 1
user2 = create(:user)
- 1
params = {name: "project addUsers test",content: "addUsers test"}
- 1
project = Project.new(params)
- 1
project.users << register
- 1
project.save
#post :create,content: "project content 3",name: "project3"
- 1
post :addUsers,user_id: user2.id,project_id: project.id
end
end
- 1
describe "GET #detail" do
- 1
it "can return correct json format of project " do
- 1
register = create(:user)
- 1
user = User.find_by(email: register.email)
- 1
session[:user_id] = user.id
- 1
user1 = create(:user)
- 1
user2 = create(:user)
- 1
params = {name: "project addUsers test",content: "addUsers test"}
- 1
project = Project.new(params)
- 1
project.users << register
- 1
project.save
- 1
post :addUsers,user_id: user2.id,project_id: project.id
post :addUsers,user_id: user1.id,project_id: project.id
post :detail,id: project.id
puts response.body
end
end
end
- 1
require 'rails_helper'
- 1
RSpec.describe SessionsController, type: :controller do
- 1
before :each do
- 11
@user1 = create(:user)
end
- 1
describe 'POST #create' do
# 合法流程校验
- 1
context 'with legal account' do
- 1
before :each do
- 3
post :create, email: @user1.email, password: @user1.password
end
- 1
it 'should get correct email and password' do
- 1
actual_email = @user1.email
- 1
actual_password = @user1.password
- 1
expect(controller.params[:email]).to eq(actual_email)
- 1
expect(controller.params[:password]).to eq(actual_password)
end
- 1
it 'should authenticate success' do
- 1
expect(controller.session[:user_id]).to eq @user1.id
end
- 1
it 'should get ok and text: ok' do
- 1
expect(response).to have_http_status :ok
- 1
expect(response.body).to eq 'ok'
end
end
# 非法参数测试
- 1
context 'with illegal account' do
- 1
before :each do
- 3
post :create, email: @user1.email, password: 'wrong_password'
end
- 1
it 'does not authenticate success' do
- 1
expect(controller.session[:user_id].nil?).to be true
end
- 1
it 'return with 401' do
- 1
expect(response).to have_http_status :unauthorized
- 1
expect(response.body).to eq 'account or password is not correct'
end
- 1
it 'should not raise error without param email or password' do
- 1
expect {
- 1
post :create, email: @user1.email
}.not_to raise_error
- 1
expect {
- 1
post :create, password: 'wrong_password'
}.not_to raise_error
end
end
end
- 1
describe 'DELETE #destroy' do
- 1
it 'should destroy user id in session' do
- 1
post :create, email:@user1.email, password: @user1.password
- 1
expect(controller.session[:user_id].nil?).to be false
- 1
delete :destroy
- 1
expect(controller.session[:user_id].nil?).to be true
end
- 1
it 'should return 401 without login' do
- 1
delete :destroy
- 1
expect(response).to have_http_status :unauthorized
end
end
- 1
describe 'GET #show' do
- 1
context 'after login' do
- 1
before :each do
- 2
post :create, email:@user1.email, password: @user1.password
- 2
get :show, format: 'json'
end
- 1
it 'should render show template' do
- 1
expect(response).to render_template 'show'
end
- 1
it 'should return user in json' do
- 1
userInfo = assigns[:current_user]
- 1
expect(userInfo.id).to eq @user1.id
end
end
- 1
it 'should renturn 401 without login' do
- 1
get :show, format: 'json'
- 1
expect(response).to have_http_status :unauthorized
end
end
end
- 1
require 'rails_helper'
- 1
RSpec.describe SharesController,type: :controller do
- 1
describe "POST #create" do
- 1
it "create a correct share" do
- 1
register = create(:user)
- 1
user = User.find_by(email: register.email)
- 1
session[:user_id] = user.id
- 1
params = {name: "project addUsers test",content: "addUsers test"}
- 1
project = Project.new(params)
- 1
project.users << register
- 1
project.save
- 1
post :create,project_id: project.id,content: "苟利国家生死以,岂因福祸避趋之"
end
end
- 1
describe "GET #getShareList" do
- 1
it "return correct share list" do
- 1
register = create(:user)
- 1
user = User.find_by(email: register.email)
- 1
session[:user_id] = user.id
- 1
params = {name: "project test",content: " test"}
- 1
project = Project.new(params)
- 1
project.users << register
- 1
project.save
- 1
post :create,project_id: project.id,content: "苟利国家生死以,岂因福祸避趋之"
post :create,project_id: project.id,content: "too young too simple"
post :create,project_id: project.id,content: "too young too naive"
get :getShareList,project_id: project.id
puts response.body
end
end
end
- 1
require 'rails_helper'
- 1
RSpec.describe UsersController, type: :controller do
- 3
let(:user) { build(:user_with_sequence_number) }
- 5
let(:user_with_fixed_info){ create(:user)}
- 6
let(:valid_attributes){ attributes_for(:user) }
- 5
let(:invalid_attributes){ attributes_for(:user, email: nil, name: 'username2000') }
- 1
describe 'GET #emailExist' do
- 1
it 'valid email' do
- 1
get :emailExist, email: user.email
- 1
expect(response.body).to eq '0'
end
- 1
it 'duplicated email' do
- 1
user = create(:user_with_sequence_number)
- 1
get :emailExist, email: user.email
- 1
expect(response.body).to eq '1'
end
- 1
it 'should not throw exception with no email param' do
- 1
get :emailExist # 不会失败,则证明没有异常
- 1
get :emailExist, other_param: 'test'
end
end
- 1
describe 'GET #usernameExist' do
- 1
it 'valid username' do
- 1
get :usernameExist, username: user.name
- 1
expect(response.body).to eq 'not exist'
end
- 1
it 'duplicated username' do
- 1
user = create(:user_with_sequence_number)
- 1
get :usernameExist, username: user.name
- 1
expect(response.body).to eq 'exist'
end
- 1
it 'should not throw exception with no username param' do
- 1
get :usernameExist
- 1
get :usernameExist, other_param: 'test'
end
end
- 1
describe 'POST #create' do
- 1
context 'success with valid attributes' do
- 1
before :each do
- 2
post :create, user: valid_attributes
end
- 1
it 'should create user' do
- 1
expect(User.exists?(assigns[:user].id)).to be true
end
- 1
it 'should response with 201' do
- 1
expect(response).to have_http_status :created
end
end
- 1
context 'fail with invalid attributes' do
# 已经在model测试中充分验证校对条件,所以这里只对使用电子邮箱为空的非法条件
- 1
before :each do
- 2
post :create, user: invalid_attributes
end
- 1
it 'does not save the new user' do
- 1
expect(User.exists? name: 'username2000').to be false
end
- 1
it 'should return errors' do
- 1
error_message = JSON.parse response.body
- 1
expect(error_message['email'].nil?).to be false
- 1
expect(error_message['email']).not_to be_empty
end
end
- 1
context 'deal with params more or less than required' do
- 1
let(:data_to_send) { { :name => 'username', :email => '1261138729@qq.com',
:password => 'secret', :password_confirmation => 'secret',
- 2
:more_field => 'test'} }
- 1
it 'should throw exception without param[:user]' do
- 1
expect {
- 1
post :create
}.to raise_error ActionController::ParameterMissing
end
- 1
it 'should not throw exception' do
- 1
expect {
- 1
post :create, user: data_to_send, other_param: 'test'
}.to_not raise_error
end
- 1
it 'should not accept other params' do
- 1
post :create, user: data_to_send
- 1
user = assigns(:user)
- 1
expect(user.has_attribute? :more_field).to eq false
end
end
end
- 1
describe 'PATCH #update' do
- 1
context 'with valid attributes' do
- 1
before :each do
# 假设已经登录了
- 4
allow(controller).to receive(:authenticate){ true }
- 2
allow(controller).to receive(:current_user).and_return(User.find user_with_fixed_info.id)
- 2
patch :update, user: valid_attributes, format: 'json'
end
- 1
it 'should located current user' do
- 1
expect(assigns[:user]).to eq user_with_fixed_info
end
- 1
it 'should render show' do
- 1
expect(response).to render_template 'show'
end
end
- 1
context 'with invalid attributes' do
- 1
before :each do
# 假设已经登录了
- 4
allow(controller).to receive(:authenticate){ true }
- 2
allow(controller).to receive(:current_user).and_return(User.find user_with_fixed_info.id)
- 2
patch :update, user: invalid_attributes, format: 'json'
end
- 1
it 'does not change current user' do
- 1
expect(assigns[:user]).to eq user_with_fixed_info
end
- 1
it 'should return error messages and error status' do
- 1
error_message = JSON.parse response.body
- 1
expect(error_message['email'].nil?).to be false
- 1
expect(error_message['email']).not_to be_empty
end
end
- 1
it 'should login first' do
- 1
patch :update, user:valid_attributes, format: 'json'
- 1
expect(response).to have_http_status :unauthorized
end
end
end
- 1
require 'rails_helper'
- 1
RSpec.describe Comment , type: :model do
- 1
it 'has a valid comment' do
- 1
params = {name: "jaxon",email: "370403444@qq.com",password_digest: "123456",phone: "15652336366"}
- 1
user = User.new(params)
- 1
user.save
- 1
params = {content:"good job",mission_id:123,user_id: user.id}
- 1
expect(Comment.new(params)).to be_valid
end
- 1
it "content should not be empty" do
- 1
params = {name: "jaxon",email: "370403444@qq.com",password_digest: "123456",phone: "15652336366"}
- 1
user = User.new(params)
- 1
user.save
- 1
params = {content: nil,mission_id: 123,user_id: user.id}
- 1
expect(Comment.new(params)).to_not be_valid
end
- 1
it "mission_id should not be empty" do
- 1
params = {name: "jaxon",email: "370403444@qq.com",password_digest: "123456",phone: "15652336366"}
- 1
user = User.new(params)
- 1
user.save
- 1
params = {content:"good job", mission_id: nil,user_id: user.id}
- 1
expect(Comment.new(params)).to_not be_valid
end
- 1
it "content should has at least one word " do
- 1
comment_with_allspace = build(:comment_content_allspace)
- 1
comment_with_allspace.validate
- 1
expect(comment_with_allspace.errors[:content].size).to eq(1)
end
- 1
it "user should be exist" do
- 1
params = {name: "jaxon",email: "370403444@qq.com",password_digest: "123456",phone: "15652336366"}
- 1
user = User.new(params)
- 1
user.save
- 1
expect(user).to be_valid
- 1
params = {name: "project_1",content: "project content"}
- 1
project = Project.new(params)
- 1
project.users << user
- 1
project.save
- 1
expect(project).to be_valid
- 1
params = {name: "mission",deadline: "2016-10-17 10:00:00",priority: 1,status: 2,content: "mission content",project_id: project.id}
- 1
mission = Mission.new(params)
- 1
mission.save
- 1
expect(mission).to be_valid
- 1
expect(mission.errors[:project_id].size).to eq(0)
- 1
params = {content: "comment", mission_id: mission.id, user_id: user.id}
- 1
comment = Comment.new(params)
- 1
expect(comment).to be_valid
end
end
- 1
require 'rails_helper'
- 1
RSpec.describe Mission , type: :model do
- 1
it 'has a valid mission' do
- 1
params = {name: "mission1",content: "mission content",deadline:"2016-10-17 10:00:00",priority:1,status:"进行中"}
- 1
mission = Mission.new(params)
- 1
mission.validate
- 1
expect(mission).to be_valid
end
- 1
it 'content should not be nil' do
- 1
expect(build(:no_content_mission)).to_not be_valid
end
- 1
it 'deadline should not be nil' do
- 1
expect(build(:no_deadline_mission)).to_not be_valid
end
- 1
it 'status should not be nil' do
- 1
expect(build(:no_status_mission)).to_not be_valid
end
- 1
it 'priority should not be nil' do
- 1
expect(build(:no_priority_mission)).to_not be_valid
end
- 1
it 'priority is not among valid values' do
- 1
mission = build(:priority_not_among_valid_values)
- 1
mission.validate
- 1
expect(mission.errors[:priority].size).to eq(1)
end
- 1
it 'status is not among valid values' do
- 1
mission = build(:status_not_among_valid_values)
- 1
mission.validate
- 1
expect(mission.errors[:status].size).to eq(1)
end
end
# require 'rails_helper'
# RSpec.describe Mission, type: :model do
# it 'has a valid mission' do
# expect(build(:project)).to be_valid
# end
# describe 'users test' do
# it 'has at least on user' do
# mission_with_no_user = build(:mission)
# mission_with_no_user.users = []
# mission_with_no_user.valid?
# expect(mission_with_no_user.errors[:users].size).to eq(1)
# end
# end
# describe 'name test' do
# it 'does not allow absence of name' do
# expect(build(:mission, name: nil)).to_not be_valid
# end
# it 'length of name in [1,20]' do
# expect(build(:mission_name_length_gt_20)).to_not be_valid
# end
# end
# end
- 1
require 'rails_helper'
- 1
RSpec.describe Missions_user , type: :model do
- 1
it "mission should exist" do
- 1
params = {name: "jaxon",email: "370403444@qq.com",password_digest: "123456",phone: "15652336366"}
- 1
user = User.new(params)
- 1
user.save
- 1
expect(user).to be_valid
- 1
params = {name: "project_1",content: "project content"}
- 1
project = Project.new(params)
- 1
project.users << user
- 1
project.save
- 1
expect(project).to be_valid
- 1
params = {name: "mission",deadline: "2016-10-17 10:00:00",priority: 1,status: 2,content: "mission content",project_id: project.id}
- 1
mission = Mission.new(params)
- 1
mission.save
- 1
expect(mission).to be_valid
- 1
params = {mission_id: 5,user_id: user.id}
- 1
missions_user = Missions_user.new(params)
- 1
missions_user.save
- 1
expect(missions_user).to_not be_valid
- 1
expect(missions_user.errors[:mission].size).to eq(1)
- 1
expect(Missions_user.find_by(user_id: user.id)).to eq(nil)
end
- 1
it "user should exist" do
- 1
params = {name: "jaxon",email: "370403444@qq.com",password_digest: "123456",phone: "15652336366"}
- 1
user = User.new(params)
- 1
user.save
- 1
expect(user).to be_valid
- 1
params = {name: "project_1",content: "project content"}
- 1
project = Project.new(params)
- 1
project.users << user
- 1
project.save
- 1
expect(project).to be_valid
- 1
params = {name: "mission",deadline: "2016-10-17 10:00:00",priority: 1,status: 2,content: "mission content",project_id: project.id}
- 1
mission = Mission.new(params)
- 1
mission.save
- 1
expect(mission).to be_valid
- 1
params = {mission_id: mission.id,user_id: 2}
- 1
missions_user = Missions_user.new(params)
- 1
missions_user.save
#puts missions_user.inspect
- 1
expect(missions_user).to_not be_valid
- 1
expect(missions_user.errors[:user].size).to eq(1)
- 1
expect(Missions_user.find_by(mission_id: mission.id)).to eq(nil)
end
end
- 1
require 'rails_helper'
- 1
RSpec.describe Note , type: :model do
- 1
it "has a valid note" do
- 1
params = {name: "jaxon",email: "370403444@qq.com",password_digest: "123456",phone: "15652336366"}
- 1
user = User.new(params)
- 1
user.save
- 1
params = {name: "project_1",content: "project content"}
- 1
project = Project.new(params)
- 1
project.users << user
- 1
project.save
- 1
expect(project).to be_valid
- 1
params = {content: "note content",user_id: user.id,project_id: project.id,category: 1}
- 1
note = Note.new(params)
- 1
note.save
- 1
expect(note).to be_valid
end
- 1
it "notes content should at least has one character" do
- 1
params = {name: "jaxon",email: "370403444@qq.com",password_digest: "123456",phone: "15652336366"}
- 1
user = User.new(params)
- 1
user.save
- 1
params = {content: " ",user_id: user.id,category: 1}
- 1
note = Note.new(params)
- 1
note.save
- 1
expect(note.errors[:content].size).to eq(2)
end
- 1
it "category should be among valid values" do
- 1
params = {name: "jaxon",email: "370403444@qq.com",password_digest: "123456",phone: "15652336366"}
- 1
user = User.new(params)
- 1
user.save
- 1
params = {content: " ",user_id: user.id,category: 4}
- 1
note = Note.new(params)
- 1
note.save
- 1
expect(note.errors[:category].size).to eq(1)
- 1
expect(note).to_not be_valid
end
- 1
it "notes user should be exist" do
- 1
params = {name: "jaxon",email: "370403444@qq.com",password_digest: "123456",phone: "15652336366"}
- 1
user = User.new(params)
- 1
user.save
- 1
params = {name: "project_1",content: "project content"}
- 1
project = Project.new(params)
- 1
project.users << user
- 1
project.save
- 1
expect(project).to be_valid
- 1
params = {content: "note content",user_id: user.id,project_id: 5,category: 1}
- 1
note = Note.new(params)
- 1
note.save
- 1
expect(note).to_not be_valid
- 1
expect(note.errors[:project].size).to eq(1)
- 1
expect(Note.find_by(project_id: 5)).to eq(nil)
end
- 1
it "notes project should be exist" do
- 1
params = {name: "jaxon",email: "370403444@qq.com",password_digest: "123456",phone: "15652336366"}
- 1
user = User.new(params)
- 1
user.save
- 1
params = {name: "project_1",content: "project content"}
- 1
project = Project.new(params)
- 1
project.users << user
- 1
project.save
- 1
expect(project).to be_valid
- 1
params = {content: "note content",user_id: 5,project_id: project.id,category: 1}
- 1
note = Note.new(params)
- 1
note.save
- 1
expect(note).to_not be_valid
- 1
expect(note.errors[:user].size).to eq(1)
- 1
expect(Note.find_by(user_id: 5)).to eq(nil)
end
end
- 1
require 'rails_helper'
- 1
RSpec.describe Project, type: :model do
- 1
it 'has a valid factory' do
- 1
expect(build(:project)).to be_valid
end
- 1
describe 'users test' do
- 1
it 'has at least on user' do
- 1
project_with_no_user = build(:project)
- 1
project_with_no_user.users = []
- 1
project_with_no_user.valid?
- 1
expect(project_with_no_user.errors[:users].size).to eq(1)
end
end
- 1
describe 'name test' do
- 1
it 'does not allow absence of name' do
- 1
expect(build(:project, name: nil)).to_not be_valid
end
- 1
it 'length of name in [1,20]' do
- 1
expect(build(:project_name_length_gt_20)).to_not be_valid
end
- 1
it 'does not allow duplicate name' do
- 1
create(:project, name: 'project1')
- 1
expect(build(:project, name: 'project1')).to_not be_valid
end
end
end
- 1
require 'rails_helper'
- 1
RSpec.describe Share, type: :model do
- 1
it "its a valid share" do
- 1
params = {name: "jaxon",email: "370403488@qq.com",password_digest: "123456",phone: "15652336366"}
- 1
user = User.new(params)
- 1
user.save
- 1
expect(user).to be_valid
- 1
params = {name: "project_1",content: "project content"}
- 1
project = Project.new(params)
- 1
project.users << user
- 1
project.save
- 1
expect(project).to be_valid
- 1
params = {content: "share content",project_id: project.id,user_id: user.id}
- 1
share = Share.new(params)
- 1
share.save
- 1
expect(share).to be_valid
- 1
expect(Share.find_by(id: share.id)).to_not eq(nil)
end
- 1
it "share content should have at least one character" do
- 1
params = {name: "jaxon",email: "370403444@qq.com",password_digest: "123456",phone: "15652336366"}
- 1
user = User.new(params)
- 1
user.save
- 1
expect(user).to be_valid
- 1
params = {name: "project_1",content: "project content"}
- 1
project = Project.new(params)
- 1
project.users << user
- 1
project.save
- 1
expect(project).to be_valid
- 1
params = {content: " ",project_id: project.id,user_id: user.id}
- 1
share = Share.new(params)
- 1
share.save
- 1
expect(share).to_not be_valid
- 1
expect(share.errors[:content].size).to eq(2)
- 1
expect(Share.find_by(id: share.id)).to eq(nil)
end
- 1
it "share's project should be exist" do
- 1
params = {name: "jaxon",email: "370403444@qq.com",password_digest: "123456",phone: "15652336366"}
- 1
user = User.new(params)
- 1
user.save
- 1
expect(user).to be_valid
- 1
params = {name: "project_1",content: "project content"}
- 1
project = Project.new(params)
- 1
project.users << user
- 1
project.save
- 1
expect(project).to be_valid
- 1
params = {content: "share content",project_id: 5,user_id: user.id}
- 1
share = Share.new(params)
- 1
share.save
- 1
expect(share).to_not be_valid
- 1
expect(share.errors[:project].size).to eq(1)
- 1
expect(Share.find_by(id: share.id)).to eq(nil)
end
- 1
it "share's user should be exist" do
- 1
params = {name: "jaxon",email: "370403444@qq.com",password_digest: "123456",phone: "15652336366"}
- 1
user = User.new(params)
- 1
user.save
- 1
expect(user).to be_valid
- 1
params = {name: "project_1",content: "project content"}
- 1
project = Project.new(params)
- 1
project.users << user
- 1
project.save
- 1
expect(project).to be_valid
- 1
params = {content: "share content",project_id: project.id,user_id: 10}
- 1
share = Share.new(params)
- 1
share.save
- 1
expect(share).to_not be_valid
- 1
expect(share.errors[:user].size).to eq(1)
- 1
expect(Share.find_by(id: share.id)).to eq(nil)
end
end
- 1
require 'rails_helper'
- 1
RSpec.describe User, type: :model do
- 1
it 'has a valid factory' do
- 1
expect(build(:user)).to be_valid
end
- 1
describe 'username test' do
- 1
it 'does not allow absence of username' do
- 1
expect(build(:user, name: nil)).not_to be_valid
end
- 1
it 'does not allow name length > 30' do
- 1
expect(build(:user_name_length_gt_30)).not_to be_valid
end
end
- 1
describe 'email test' do
- 1
it 'does not allow absence of email' do
- 1
expect(build(:user, email: nil)).not_to be_valid
end
- 1
it 'does not allow email length > 50' do
- 1
expect(build(:user_email_length_gt_50)).not_to be_valid
end
- 1
it 'email should match /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i' do
- 1
expect(build(:user, email: '123qq.com')).not_to be_valid
- 1
expect(build(:user, email: '@qq.com')).not_to be_valid
- 1
expect(build(:user, email: '123@qq')).not_to be_valid
- 1
expect(build(:user, email: '123@.com')).not_to be_valid
- 1
expect(build(:user, email: '123@qq.')).not_to be_valid
end
end
- 1
describe 'password test' do
- 1
it 'does not allow password length < 6' do
- 1
expect(build(:user_password_length_lt_6)).not_to be_valid
end
- 1
it 'password_confirmation should match password when password_confirmation not nil' do
- 1
expect(build(:user, password: '123456', password_confirmation: '654321')).to_not be_valid
end
- 1
it 'do not trigger match when password_confirmation nil' do
- 1
expect(build(:user, password: '123456', password_confirmation: nil)).to be_valid
end
- 1
it 'does not allow password absence on create' do
- 1
expect(build(:user, password: nil, password_confirmation: nil)).to_not be_valid
end
- 1
it 'can authenticate' do
- 1
user = create(:user, password: '123456', password_confirmation: '123456')
- 1
expect(user.authenticate('123456')).to be_instance_of User
- 1
expect(user.authenticate('654312')).to be false
end
end
- 1
describe 'phone test' do
- 1
it 'does not allow phone absence' do
- 1
expect(build(:user, phone: nil)).to_not be_valid
end
- 1
it 'does not allow character other than number' do
- 1
expect(build(:user, phone: '1371234567a')).to_not be_valid
end
- 1
it 'does not allow phone longer or shorter than 11' do
- 1
expect(build(:user, phone: '137123456789')).to_not be_valid
- 1
expect(build(:user, phone: '1371234567')).to_not be_valid
end
# 防js注入
- 1
it 'does not allow multiline' do
- 1
expect(build(:user, phone: '13712345\n78')).to_not be_valid
end
end
end