Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] WIP: controller test #36

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
version: 2.1
orbs:
ruby: circleci/[email protected]

jobs:
build:
docker:
- image: circleci/ruby:2.6.6-stretch-node
executor: ruby/default
steps:
- checkout
- run:
name: Install bundler
command: |
gem update --system
gem install bundler:2.1.4
# - ruby/bundle-install
- run:
name: Bundle install
command: bundle install
- run:
name: Which bundler?
command: bundle -v
- run:
name: Run rails tests
command: bundle exec rails test
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ GEM
mini_mime (1.0.2)
mini_portile2 (2.5.0)
minitest (5.14.3)
msgpack (1.4.0)
msgpack (1.4.2)
multi_json (1.15.0)
multi_xml (0.6.0)
multipart-post (2.1.1)
Expand Down
1 change: 1 addition & 0 deletions app/helpers/authentication_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def current_user
@current_user ||= User.find(session[:user_id])
elsif cookies[:_idobata_token]
# Use cookie token
puts cookies.signed
puts "cookie", cookies.signed[:_idobata_token]
token_uuid = cookies.signed[:_idobata_token]
@current_user = Token.user_by_token(token_uuid)
Expand Down
2 changes: 2 additions & 0 deletions app/models/favorite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class Favorite < ApplicationRecord
has_one :notification, dependent: :destroy
before_save :create_notification
after_save :save_notification
validates :user, presence: true, uniqueness: { scope: [:user, :post] }
validates :post, presence: true, uniqueness: { scope: [:user, :post] }

private

Expand Down
1 change: 1 addition & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Post < ApplicationRecord

validate :post_without_tags_is_disallowed
validates :content, length: { minimum: 1, maximum: Rails.configuration.x.preferences.post[:postMaxLetters] }
validates :color, presence: true

before_save :content_processing
before_save :define_column_size
Expand Down
8 changes: 4 additions & 4 deletions test/channels/notification_channel_test.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require "test_helper"

class NotificationChannelTest < ActionCable::Channel::TestCase
# test "subscribes" do
# subscribe
# assert subscription.confirmed?
# end
test "subscribes" do
subscribe
assert subscription.confirmed?
end
end
12 changes: 7 additions & 5 deletions test/controllers/api/v1/posts_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,36 @@
class API::V1::PostsControllerTest < ActionDispatch::IntegrationTest
setup do
@post = posts(:one)
cookies.signed[:_idobata_token] = get_token
puts 'token:', cookies.signed.to_hash
end

test "should get index" do
get posts_url, as: :json
get api_v1_posts_url, as: :json
assert_response :success
end

test "should create post" do
assert_difference('Post.count') do
post posts_url, params: { post: { content: @post.content, title: @post.title, user: @post.user } }, as: :json
post api_v1_posts_url, params: { post: { content: @post.content } }, as: :json
end

assert_response 201
end

test "should show post" do
get post_url(@post), as: :json
get api_v1_post_url(@post), as: :json
assert_response :success
end

test "should update post" do
patch post_url(@post), params: { post: { content: @post.content, title: @post.title, user: @post.user } }, as: :json
patch api_v1_post_url(@post), params: { post: { content: @post.content } }, as: :json
assert_response 200
end

test "should destroy post" do
assert_difference('Post.count', -1) do
delete post_url(@post), as: :json
delete api_v1_post_url(@post), as: :json
end

assert_response 204
Expand Down
4 changes: 2 additions & 2 deletions test/controllers/api/v1/tags_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ class Api::V1::TagsControllerTest < ActionDispatch::IntegrationTest
# assert true
# end

test "shoud create tag" do
get tags_url
test "should create tag" do
get api_v1_tags_url
assert_response :success
end
end
20 changes: 20 additions & 0 deletions test/controllers/api/v1/user_settings_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'test_helper'

class Api::V1::UserSettingsControllerTest < ActionDispatch::IntegrationTest
setup do
@user_setting = user_settings(:one)
end

test "should get index" do
get api_v1_user_settings_url, as: :json
assert_response :success
end

test "should create user_setting" do
assert_difference('UserSetting.count') do
post api_v1_user_settings_url, params: { user_setting: { setting: @user_setting.setting } }, as: :json
end

assert_response 201
end
end
38 changes: 0 additions & 38 deletions test/controllers/user_settings_controller_test.rb

This file was deleted.

14 changes: 14 additions & 0 deletions test/cookies_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Rack::Test::CookieJar

def signed
self
end

def permanent
self
end

def encrypted
self
end
end
8 changes: 4 additions & 4 deletions test/fixtures/favorites.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
user: one
post: one
user_id: 1
post_id: 1

two:
user: two
post: two
user_id: 2
post_id: 1
6 changes: 4 additions & 2 deletions test/fixtures/notifications.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
favorite_id: one
favorite_id: 1
user_id: 1

two:
favorite_id: two
favorite_id: 2
user_id: 2
16 changes: 8 additions & 8 deletions test/fixtures/posts.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
title: MyString
content: MyText
user:
post_one:
id: 1
content: foobar
user: user_one

two:
title: MyString
content: MyText
user:
post_two:
id: 2
content: hogefuga
user: user_one
20 changes: 14 additions & 6 deletions test/fixtures/taggings.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
post:
tag:
tagging_one_one:
post: post_one
tag: tag_one

two:
post:
tag:
tagging_one_two:
post: post_one
tag: tag_two

tagging_two_one:
post: post_one
tag: tag_two

tagging_two_two:
post: post_two
tag: tag_two
14 changes: 6 additions & 8 deletions test/fixtures/tags.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
name: MyString
created_by: MyString
references: MyString
tag_one:
name: test-tag-1
created_by: user_one

two:
name: MyString
created_by: MyString
references: MyString
tag_two:
name: test-tag-2
created_by: user_two
9 changes: 5 additions & 4 deletions test/fixtures/tokens.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
user: one
uuid: MyString
user_id: 1
uuid: hogefuga


two:
user: two
uuid: MyString
user_id: 2
uuid: fugahoge
16 changes: 14 additions & 2 deletions test/fixtures/users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,20 @@
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
user_one:
provider: discord
uid: 123456789
name: test-01
nickname: test-01-nickname
image: https://picsum.photos/200/200
email: [email protected]
# column: value
#
two: {}
user_two:
provider: discord
uid: 987654321
name: test-02
nickname: test-02-nickname
image: https://picsum.photos/200/200
email: [email protected]
# column: value
18 changes: 18 additions & 0 deletions test/models/favorite_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,22 @@ class FavoriteTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end

test 'favorite post by user' do
favorite = posts(:post_one).favorites.build(user: users(:user_one))
assert favorite.valid?
assert favorite.save
end

test 'favorite must not duplicated' do
# First favorite
favorite = posts(:post_one).favorites.build(user: users(:user_one))
assert favorite.valid?
assert favorite.save

# Duplicated favorite (must be rejected)
favorite = posts(:post_one).favorites.build(user: users(:user_one))
assert_not favorite.valid?
assert_not favorite.save
end
end
13 changes: 13 additions & 0 deletions test/models/notification_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,17 @@ class NotificationTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end

test 'create notification by favorite' do
def notification_count
Notification.where(user: posts(:post_one).user).count
end

before_notification_count = notification_count

favorite = posts(:post_one).favorites.build(user: users(:user_one))

assert notification_count - before_notification_count == 1
assert Notification.where(user: posts(:post_one).user, favorite: favorite).count == 1
end
end
16 changes: 16 additions & 0 deletions test/models/post_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,20 @@ class PostTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
test 'create post' do
post = Post.new(content: 'foobar', user: users(:user_one), color: '#FF0000', tags: [tags(:tag_one), tags(:tag_two)])
assert post.valid?
assert post.save
end

test 'post must have some tags' do
post = Post.new(content: 'foobar', user: users(:user_one), color: '#FF0000', tags: [])
assert_not post.valid?
end

test 'post must have color' do
post = Post.new(content: 'foobar', user: users(:user_one), tags: [tags(:tag_one), tags(:tag_two)])
assert_not post.valid?
assert_not post.save
end
end
Loading