From d8a1f240c4ebd3e8391e83e2a383bfcc19da381c Mon Sep 17 00:00:00 2001 From: yasushiito Date: Tue, 2 Oct 2012 18:46:46 +0900 Subject: [PATCH] t#29505:md5 search wrote test on picture --- app/controllers/pictures_controller.rb | 12 ++- app/models/picture.rb | 9 ++ app/views/original_pictures/_history_list.html.erb | 2 +- app/views/pictures/md5.html.erb | 0 config/routes.rb | 1 + spec/controllers/pictures_controller_spec.rb | 98 ++++++++++++++++++++++ spec/models/picture_spec.rb | 29 +++++++ 7 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 app/views/pictures/md5.html.erb diff --git a/app/controllers/pictures_controller.rb b/app/controllers/pictures_controller.rb index 64c1d9fd..850ef318 100644 --- a/app/controllers/pictures_controller.rb +++ b/app/controllers/pictures_controller.rb @@ -1,6 +1,7 @@ class PicturesController < ApplicationController layout 'test' if Pettanr::Application.test_layout - before_filter :authenticate_user!, :only => [:show, :credit] + before_filter :authenticate_user!, :only => [:show, :credit, :md5] + before_filter :authenticate_artist, :only => [:md5] def show @picture = Picture.show(params[:id], @author) @@ -24,4 +25,13 @@ class PicturesController < ApplicationController end end + def md5 + @pictures = Picture.list_by_md5(params[:md5]) + + respond_to do |format| + format.html + format.json { render json: @pictures.to_json } + end + end + end diff --git a/app/models/picture.rb b/app/models/picture.rb index 9701cf8f..a4b9e38b 100644 --- a/app/models/picture.rb +++ b/app/models/picture.rb @@ -52,6 +52,11 @@ class Picture < ActiveRecord::Base '/pictures/' + filename end + def opt_img_tag + tw, th = PettanImager.thumbnail_size(self.width, self.height) + {:src => self.url, :width => tw, :height => th} + end + def new_revision Picture.maximum(:revision, :conditions => ['original_picture_id = ?', self.original_picture_id]).to_i + 1 end @@ -69,6 +74,10 @@ class Picture < ActiveRecord::Base flag_reverse < 0 ? [''] : ['', 'v', 'h', 'vh'] end + def self.find_by_md5 md5 + r = Picture.find :all, :conditions => ['pictures.md5 = ?', md5], :order => 'pictures.updated_at desc' + end + def self.list_by_md5 md5, opid r = Picture.find :all, :conditions => ['pictures.md5 = ? and pictures.original_picture_id <> ?', md5, opid], :order => 'pictures.updated_at desc' end diff --git a/app/views/original_pictures/_history_list.html.erb b/app/views/original_pictures/_history_list.html.erb index 8fd4c22e..dfd0a52f 100644 --- a/app/views/original_pictures/_history_list.html.erb +++ b/app/views/original_pictures/_history_list.html.erb @@ -1,6 +1,6 @@ <% history.each do |picture| -%>

revision:<%= picture.revision %>

licensed at <%= picture.created_at %>
- + <%= tag(:img, picture.opt_img_tag) -%> <%= render picture.credit_template, :picture => picture %> <% end -%> diff --git a/app/views/pictures/md5.html.erb b/app/views/pictures/md5.html.erb new file mode 100644 index 00000000..e69de29b diff --git a/config/routes.rb b/config/routes.rb index f984d8ef..ac3e8d8c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -83,6 +83,7 @@ Pettanr::Application.routes.draw do resources :pictures do collection do get :show + get :md5 end member do get :credit diff --git a/spec/controllers/pictures_controller_spec.rb b/spec/controllers/pictures_controller_spec.rb index 03cd7de2..6fc7a8f0 100644 --- a/spec/controllers/pictures_controller_spec.rb +++ b/spec/controllers/pictures_controller_spec.rb @@ -242,4 +242,102 @@ describe PicturesController do =end end + describe 'md5検索の一覧に於いて' do + before do + @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id + sign_in @user + Picture.stub(:list_by_md5).with(any_args).and_return([@p, @p, @p]) + end + context 'つつがなく終わるとき' do + it '実素材モデルにmd5検索を問い合わせている' do + Picture.should_receive(:list_by_md5).exactly(1) + get :md5, :md5 => 'a'*32 + end + it '@picturesにリストを取得している' do + get :md5, :md5 => 'a'*32 + assigns(:pictures).should have_at_least(3).items + end + context 'html形式' do + it 'ステータスコード200 OKを返す' do + get :md5, :md5 => 'a'*32 + response.should be_success + end + it 'md5テンプレートを描画する' do + get :md5, :md5 => 'a'*32 + response.should render_template("md5") + end + end + context 'json形式' do + it 'ステータスコード200 OKを返す' do + get :md5, :md5 => 'a'*32, :format => :json + response.should be_success + end + it 'jsonデータを返す' do + get :md5, :md5 => 'a'*32, :format => :json + lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) + end + it 'データがリスト構造になっている' do + get :md5, :md5 => 'a'*32, :format => :json + json = JSON.parse response.body + json.should have_at_least(3).items + end + it 'リストの先頭くらいは実素材っぽいものであって欲しい' do + get :md5, :md5 => 'a'*32, :format => :json + json = JSON.parse response.body + json.first.has_key?("ext").should be_true + json.first.has_key?("md5").should be_true + json.first.has_key?("artist_id").should be_true + json.first.has_key?("width").should be_true + end + end + end + context '作家権限がないとき' do + before do + sign_out @user + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + get :md5, :md5 => 'a'*32 + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + get :md5, :md5 => 'a'*32 + response.should redirect_to '/users/sign_in' + end + end + context 'json形式' do + it 'ステータスコード401 Unauthorizedを返す' do + get :md5, :md5 => 'a'*32, :format => :json + response.status.should eq 401 + end + it '応答メッセージにUnauthorizedを返す' do + get :md5, :md5 => 'a'*32, :format => :json + response.message.should match(/Unauthorized/) + end + end + end + context '作家が絵師でないとき' do + before do + Author.any_instance.stub(:artist?).and_return(false) + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + get :md5, :md5 => 'a'*32 + response.status.should eq 302 + end + it '絵師登録ページへ遷移する' do + get :md5, :md5 => 'a'*32 + response.should redirect_to new_artist_path + end + end + context 'json形式' do + it '例外403 forbiddenを返す' do + lambda{ + get :md5, :md5 => 'a'*32, :format => :json + }.should raise_error(ActiveRecord::Forbidden) + end + end + end + end + end diff --git a/spec/models/picture_spec.rb b/spec/models/picture_spec.rb index 002c7162..45af3809 100644 --- a/spec/models/picture_spec.rb +++ b/spec/models/picture_spec.rb @@ -423,6 +423,35 @@ describe Picture do end end + describe 'md5検索に於いて' do + before do + @op = FactoryGirl.create :original_picture, :artist_id => @artist.id + @p = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 0, + :original_picture_id => @op.id, :md5 => 'a' * 32 + @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id + @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id + @p2 = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 0, + :original_picture_id => @op2.id, :md5 => 'b' * 32 + @op3 = FactoryGirl.create :original_picture, :artist_id => @artist.id + end + it 'リストを返す' do + res = Picture.find_by_md5(@p.md5) + res.is_a?(Array).should be_true + end + it 'md5が違えば含まない' do + res = Picture.find_by_md5(@p.md5) + res.include?(@p2).should be_false + end + it '更新日時順' do + @p3 = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 1, + :original_picture_id => @op2.id, :md5 => 'C' * 32 + @p4 = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 1, + :original_picture_id => @op3.id, :md5 => @p3.md5, :updated_at => Time.now + 100 + res = Picture.find_by_md5(@p3.md5) + res.should eq [@p4, @p3] + end + end + describe 'md5重複リストに於いて' do before do @op = FactoryGirl.create :original_picture, :artist_id => @artist.id -- 2.11.0