OSDN Git Service

Merge branch 'v06' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v06
[pettanr/pettanr.git] / spec / models / license_spec.rb
index 5f7f4d0..0889793 100644 (file)
@@ -12,33 +12,36 @@ describe License do
       @l = FactoryGirl.build :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
     end
     
-    it 'オーソドックスなデータなら通る' do
-      @l.should be_valid
+    context 'オーソドックスなデータのとき' do
+      it '下限データが通る' do
+        @l.name = 'a'
+        @l.caption = 'a'
+        @l.url = 'http://test.jp/'
+        @l.should be_valid
+      end
+      it '上限データが通る' do
+        @l.name = 'a'*50
+        @l.caption = 'a'*30
+        @l.url = 'http://test.jp/aaaaa' + 'a' * 180
+        @l.should be_valid
+      end
     end
     
     context 'license_group_idを検証するとき' do
-      it 'テストデータの確認' do\r
-        @l.license_group_id = @lg.id\r
-        @l.should be_valid\r
-      end\r
       it 'nullなら失敗する' do
         @l.license_group_id = ''
         @l.should_not be_valid
       end
-      it '数値でなければ失敗する' do\r
-        @l.license_group_id = 'a'\r
-        @l.should_not be_valid\r
-      end\r
-      it '存在するライセンスグループでなければ失敗する' do\r
-        @l.license_group_id = 0\r
-        @l.should_not be_valid\r
-      end\r
+      it '数値でなければ失敗する' do
+        @l.license_group_id = 'a'
+        @l.should_not be_valid
+      end
+      it '存在するライセンスグループでなければ失敗する' do
+        @l.license_group_id = 0
+        @l.should_not be_valid
+      end
     end
     context 'nameを検証するとき' do
-      it 'テストデータの確認' do\r
-        @l.name = 'a'*50\r
-        @l.should be_valid\r
-      end\r
       it 'nullなら失敗する' do
         @l.name = ''
         @l.should_not be_valid
@@ -53,10 +56,6 @@ describe License do
       end
     end
     context 'captionを検証するとき' do
-      it 'テストデータの確認' do\r
-        @l.caption = 'a'*30\r
-        @l.should be_valid\r
-      end\r
       it 'nullなら失敗する' do
         @l.caption = ''
         @l.should_not be_valid
@@ -67,11 +66,6 @@ describe License do
       end
     end
     context 'urlを検証するとき' do
-      it 'テストデータの確認' do\r
-        @l.url = 'http://test.jp/aaaaa' + 'a' * 180
-        @l.save!\r
-        @l.should be_valid\r
-      end\r
       it 'nullなら失敗する' do
         @l.url = ''
         @l.should_not be_valid
@@ -80,28 +74,222 @@ describe License do
         @l.url = 'http://test.jp/aaaaa' + 'a' * 181
         @l.should_not be_valid
       end
-      it 'url形式でなら失敗する' do
-        @l.url = 'a'*200\r
-        @l.should_not be_valid\r
+      it 'urlå½¢å¼\8fã\81§ã\81ªã\81\84ã\81ªã\82\89失æ\95\97ã\81\99ã\82\8b' do
+        @l.url = 'a'*200
+        @l.should_not be_valid
       end
     end
     context 'system_picture_idを検証するとき' do
-      it 'テストデータの確認' do\r
-        @l.system_picture_id = @sp.id\r
-        @l.should be_valid\r
-      end\r
       it 'nullなら失敗する' do
         @l.system_picture_id = ''
         @l.should_not be_valid
       end
-      it '数値でなければ失敗する' do\r
-        @l.system_picture_id = 'a'\r
-        @l.should_not be_valid\r
-      end\r
-      it '存在するシステム画像でなければ失敗する' do\r
-        @l.system_picture_id = 0\r
-        @l.should_not be_valid\r
-      end\r
+      it '数値でなければ失敗する' do
+        @l.system_picture_id = 'a'
+        @l.should_not be_valid
+      end
+      it '存在するシステム画像でなければ失敗する' do
+        @l.system_picture_id = 0
+        @l.should_not be_valid
+      end
+    end
+  end
+  
+  describe '文字コード検証に於いて' do
+    before do
+      @sp = FactoryGirl.create :system_picture
+      @lg = FactoryGirl.create :license_group
+      @l = FactoryGirl.build :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
+    end
+    
+    context 'nameを検証するとき' do
+      it 'Shift JISなら失敗する' do
+        @l.name = "\x83G\x83r\x83]\x83D"
+        lambda{
+          @l.valid_encode
+        }.should raise_error(Pettanr::BadRequest)
+      end
+    end
+    
+    context 'captionを検証するとき' do
+      it 'Shift JISなら失敗する' do
+        @l.caption = "\x83G\x83r\x83]\x83D"
+        lambda{
+          @l.valid_encode
+        }.should raise_error(Pettanr::BadRequest)
+      end
+    end
+    
+    context 'urlを検証するとき' do
+      it 'Shift JISなら失敗する' do
+        @l.url = "\x83G\x83r\x83]\x83D"
+        lambda{
+          @l.valid_encode
+        }.should raise_error(Pettanr::BadRequest)
+      end
+    end
+    
+    context 'settingsを検証するとき' do
+      it 'Shift JISなら失敗する' do
+        @l.settings = "\x83G\x83r\x83]\x83D"
+        lambda{
+          @l.valid_encode
+        }.should raise_error(Pettanr::BadRequest)
+      end
+    end
+    
+    context 'credit_picturesを検証するとき' do
+      it 'Shift JISなら失敗する' do
+        @l.credit_pictures = "\x83G\x83r\x83]\x83D"
+        lambda{
+          @l.valid_encode
+        }.should raise_error(Pettanr::BadRequest)
+      end
+    end
+    
+  end
+  
+  describe 'デフォルト値補充に於いて' do
+    it 'defined' do
+      @sp = FactoryGirl.create :system_picture
+      @lg = FactoryGirl.create :license_group
+      @l = FactoryGirl.build :license, :name => 'peta2.0', :license_group_id => @lg.id, :system_picture_id => @sp.id
+      @l.supply_default
+    end
+  end
+  
+  describe '上書き補充に於いて' do
+    it 'defined' do
+      @sp = FactoryGirl.create :system_picture
+      @lg = FactoryGirl.create :license_group
+      @l = FactoryGirl.build :license, :name => 'peta2.0', :license_group_id => @lg.id, :system_picture_id => @sp.id
+      @l.overwrite
+    end
+  end
+  
+  describe '閲覧許可に於いて' do
+    #ライセンスは作家作成する前から存在するので、閲覧制限の意味がない
+  end
+  
+  describe '一覧取得に於いて' do
+    before do
+      @sp = FactoryGirl.create :system_picture
+      @lg = FactoryGirl.create :license_group
+      @l = FactoryGirl.create :license, :name => 'peta2.0', :license_group_id => @lg.id, :system_picture_id => @sp.id
+      @lg2 = FactoryGirl.create :license_group, :name => 'pubdm'
+    end
+    context 'つつがなく終わるとき' do
+      it '一覧取得オプションを利用している' do
+        License.stub(:list_opt).with(any_args).and_return({})
+        License.should_receive(:list_opt).with(any_args).exactly(1)
+        r = License.list
+      end
+    end
+    it 'リストを返す' do
+      l = License.list 
+      l.should eq [@l]
+    end
+    it '名前順で並んでいる' do
+      @l2 = FactoryGirl.create :license, :name => 'peta3.0', :url => 'http://pe.ta/3.0', :license_group_id => @lg.id, :system_picture_id => @sp.id
+      @l3 = FactoryGirl.create :license, :name => 'pd1.0', :url => 'http://pb.dm/1.0', :license_group_id => @lg2.id, :system_picture_id => @sp.id
+      l = License.list
+      l.should eq [@l3, @l, @l2]
+    end
+  end
+  describe '一覧取得オプションに於いて' do
+    it 'includeキーを含んでいる' do
+      r = License.list_opt
+      r.has_key?(:include).should be_true
+    end
+    it '1つの項目を含んでいる' do
+      r = License.list_opt[:include]
+      r.should have(1).items
+    end
+    it 'ライセンスグループを含んでいる' do
+      r = License.list_opt[:include]
+      r.has_key?(:license_group).should be_true
+    end
+  end
+  describe 'json一覧出力オプションに於いて' do
+    before do
+      @sp = FactoryGirl.create :system_picture
+      @lg = FactoryGirl.create :license_group
+      @l = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
+    end
+    it 'ライセンスグループを含んでいる' do
+      r = License.list.to_json License.list_json_opt
+      j = JSON.parse r
+      i = j.first
+      i.has_key?('license_group').should be_true
+    end
+  end
+  
+  describe '単体取得に於いて' do
+    before do
+      @sp = FactoryGirl.create :system_picture
+      @lg = FactoryGirl.create :license_group
+      @l = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
+    end
+    context 'つつがなく終わるとき' do
+      it '単体取得オプションを利用している' do
+        License.stub(:show_opt).with(any_args).and_return({})
+        License.should_receive(:show_opt).with(any_args).exactly(1)
+        r = License.show @l.id
+      end
+    end
+    it '指定のライセンスを返す' do
+      l = License.show @l.id
+      l.should eq @l
+    end
+    context '存在しないライセンスを開こうとしたとき' do
+      it '404RecordNotFound例外を返す' do
+        lambda{
+          License.show 110
+        }.should raise_error(ActiveRecord::RecordNotFound)
+      end
+    end
+  end
+  describe '単体取得オプションに於いて' do
+    it 'includeキーを含んでいる' do
+      r = License.show_opt
+      r.has_key?(:include).should be_true
+    end
+    it '1つの項目を含んでいる' do
+      r = License.show_opt[:include]
+      r.should have(1).items
+    end
+    it 'ライセンスグループを含んでいる' do
+      r = License.show_opt[:include]
+      r.has_key?(:license_group).should be_true
+    end
+  end
+  describe 'json単体出力オプションに於いて' do
+    before do
+      @sp = FactoryGirl.create :system_picture
+      @lg = FactoryGirl.create :license_group
+      @l = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
+    end
+    it 'ライセンスグループを含んでいる' do
+      r = License.show(@l.id).to_json License.show_json_opt
+      j = JSON.parse r
+      i = j
+      i.has_key?('license_group').should be_true
+    end
+  end
+  
+  describe '管理名検索に於いて' do
+    before do
+      @sp = FactoryGirl.create :system_picture
+      @lg = FactoryGirl.create :license_group
+      @l = FactoryGirl.create :license, :name => 'peta2.0', :license_group_id => @lg.id, :system_picture_id => @sp.id
+      @lg2 = FactoryGirl.create :license_group, :name => 'pubdm'
+      @l2 = FactoryGirl.create :license, :name => 'pettan2.2b', :url => @l.url + 'l2', :license_group_id => @lg.id, :system_picture_id => @sp.id
+    end
+    context 'つつがなく終わるとき' do
+      it 'リストを返す' do
+        l = License.list_by_name @l2.name
+        l.should eq [@l2]
+      end
     end
   end
   
@@ -145,6 +333,9 @@ describe License do
         r.is_a?(License).should be_true
         r.name.should eq @ln
         r.url.should eq @la["url"]
+        cp = JSON.parse r.credit_pictures
+        cp['a_picture_id'].should_not be_nil
+        cp['b_picture_id'].should_not be_nil
       end
       it 'ライセンスが作成されている' do
         lambda {
@@ -220,89 +411,20 @@ describe License do
     end
   end
   
-  describe '単体取得に於いて' do
+  describe 'エクスポートに於いて' do
     before do
       @sp = FactoryGirl.create :system_picture
       @lg = FactoryGirl.create :license_group
       @l = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
+      @l2 = FactoryGirl.create :license, :name => 'l2[old ls]', :url => @l.url+'2', :license_group_id => @lg.id, :system_picture_id => @sp.id, :updated_at => Time.now - 3000
     end
-    it '指定のコマを返す' do
-      l = License.show @l.id
-      l.should eq @l
-    end
-  end
-  describe '関連テーブルプションに於いて' do
-    context 'オプションがないとき' do
-      it 'ライセンスグループを含んでいる' do
-        r = License.show_include_opt
-        r.should eq [:license_group]
-      end
-    end
-    context 'オプションで素材ライセンスを含ませたとき' do
-      it 'ライセンスグループと素材ライセンスを含んでいる' do
-        r = License.show_include_opt(:include => :resource_picture_license)
-        r.should eq [:license_group, :resource_picture_license]
-      end
-    end
-  end
-  describe 'json単体出力オプションに於いて' do
-    it 'includeキーを含んでいる' do
-      r = License.show_json_include_opt
-      r.has_key?(:include).should be_true
-    end
-    it '1つの項目を含んでいる' do
-      r = License.show_json_include_opt[:include]
-      r.should have(1).items
-    end
-    it 'ライセンスグループを含んでいる' do
-      r = License.show_json_include_opt[:include]
-      r.has_key?(:license_group).should be_true
-    end
-  end
-  describe '一覧取得に於いて' do
-    before do
-      @sp = FactoryGirl.create :system_picture
-      @lg = FactoryGirl.create :license_group
-      @l = FactoryGirl.create :license, :name => 'peta2.0', :license_group_id => @lg.id, :system_picture_id => @sp.id
-      @lg2 = FactoryGirl.create :license_group, :name => 'pubdm'
-    end
-    it 'リストを返す' do
-      l = License.list 
-      l.should eq [@l]
-    end
-    it '名前順で並んでいる' do
-      @l2 = FactoryGirl.create :license, :name => 'peta3.0', :url => 'http://pe.ta/3.0', :license_group_id => @lg.id, :system_picture_id => @sp.id
-      @l3 = FactoryGirl.create :license, :name => 'pd1.0', :url => 'http://pb.dm/1.0', :license_group_id => @lg2.id, :system_picture_id => @sp.id
-      l = License.list
-      l.should eq [@l3, @l, @l2]
-    end
-  end
-  describe 'list関連テーブルプションに於いて' do
-    it 'includeキーを含んでいる' do
-      r = License.list_opt
-      r.has_key?(:include).should be_true
+    it '開始日時が省略された場合はすべてのライセンスを返す' do
+      r = License.export 
+      r.should eq [@l, @l2]
     end
-    it '1つの項目を含んでいる' do
-      r = License.list_opt[:include]
-      r.should have(1).items
-    end
-    it 'ライセンスグループを含んでいる' do
-      r = License.list_opt[:include]
-      r.has_key?(:license_group).should be_true
-    end
-  end
-  describe 'json一覧出力オプションに於いて' do
-    it 'includeキーを含んでいる' do
-      r = License.list_json_opt
-      r.has_key?(:include).should be_true
-    end
-    it '1つの項目を含んでいる' do
-      r = License.list_json_opt[:include]
-      r.should have(1).items
-    end
-    it 'ライセンスグループを含んでいる' do
-      r = License.list_json_opt[:include]
-      r.has_key?(:license_group).should be_true
+    it '開始日時以降に更新されたライセンスを返す' do
+      r = License.export @l.updated_at - 100
+      r.should eq [@l]
     end
   end