genghisapp 2.1.6 → 2.2.0.beta.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -525,6 +525,16 @@ genghis_backends.each do |backend|
525
525
  res.status.should eq 400
526
526
  end
527
527
 
528
+ it 'returns 400 if the document id is invalid' do
529
+ res = @api.post do |req|
530
+ req.url '/servers/localhost/databases/__genghis_spec_test__/collections/spec_docs/documents'
531
+ req.headers['Content-Type'] = 'application/json'
532
+ req.body = {_id: [0, 1]}.to_json
533
+ end
534
+
535
+ res.status.should eq 400
536
+ end
537
+
528
538
  it 'returns 404 if the collection is not found' do
529
539
  res = @api.post do |req|
530
540
  req.url '/servers/localhost/databases/__genghis_spec_test__/collections/fake_docs/documents'
@@ -588,6 +598,17 @@ genghis_backends.each do |backend|
588
598
  test: 2
589
599
  end
590
600
 
601
+ it 'returns 400 if a document id is updated' do
602
+ id = @coll.insert({test: 1})
603
+ res = @api.put do |req|
604
+ req.url '/servers/localhost/databases/__genghis_spec_test__/collections/spec_docs/documents/' + id.to_s
605
+ req.headers['Content-Type'] = 'application/json'
606
+ req.body = { _id: 1, test: 2 }.to_json
607
+ end
608
+
609
+ res.status.should eq 400
610
+ end
611
+
591
612
  it 'returns 400 if the document is invalid' do
592
613
  id = @coll.insert({test: 1})
593
614
  res = @api.put do |req|
@@ -660,6 +681,179 @@ genghis_backends.each do |backend|
660
681
  @coll.find(_id: id).count.should eq 1
661
682
  end
662
683
  end
684
+
685
+ context 'GridFS' do
686
+ before :all do
687
+ @grid = Mongo::Grid.new(@coll.db, 'test')
688
+ @grid.put('tmp')
689
+ end
690
+
691
+ describe 'POST /servers/:server/databases/:db/collections/:coll/files' do
692
+ it 'inserts a new file' do
693
+ res = @api.post do |req|
694
+ req.url '/servers/localhost/databases/__genghis_spec_test__/collections/test.files/files'
695
+ req.headers['Content-Type'] = 'application/json'
696
+ req.body = {
697
+ file: encode_upload('foo!'),
698
+ filename: 'foo.txt',
699
+ contentType: 'application/octet',
700
+ metadata: {expected: 'you know it'}
701
+ }.to_json
702
+ end
703
+
704
+ res.status.should eq 200
705
+ res.body.should match_json_expression \
706
+ _id: Hash,
707
+ filename: 'foo.txt',
708
+ contentType: 'application/octet',
709
+ metadata: { expected: 'you know it' },
710
+ uploadDate: Hash,
711
+ length: Fixnum,
712
+ chunkSize: Fixnum,
713
+ md5: String
714
+ end
715
+
716
+ it 'returns 400 if the file upload is not a base64 encoded data: URI' do
717
+ res = @api.post do |req|
718
+ req.url '/servers/localhost/databases/__genghis_spec_test__/collections/test.files/files'
719
+ req.headers['Content-Type'] = 'application/json'
720
+ req.body = {
721
+ file: 'foo!',
722
+ filename: 'foo.txt',
723
+ contentType: 'application/octet',
724
+ metadata: {expected: 'you know it'}
725
+ }.to_json
726
+ end
727
+
728
+ res.status.should eq 400
729
+ res.body.should match_json_expression \
730
+ error: 'File must be a base64 encoded data: URI',
731
+ status: 400
732
+ end
733
+
734
+ it 'returns 400 if the document is missing important bits' do
735
+ res = @api.post do |req|
736
+ req.url '/servers/localhost/databases/__genghis_spec_test__/collections/test.files/files'
737
+ req.headers['Content-Type'] = 'application/json'
738
+ req.body = {filename: 'foo.txt'}.to_json
739
+ end
740
+ res.status.should eq 400
741
+ end
742
+
743
+ it 'returns 400 if the document has unexpected properties' do
744
+ res = @api.post do |req|
745
+ req.url '/servers/localhost/databases/__genghis_spec_test__/collections/test.files/files'
746
+ req.headers['Content-Type'] = 'application/json'
747
+ req.body = {file: encode_upload('foo'), unexpected: 'you know it.'}.to_json
748
+ end
749
+ res.status.should eq 400
750
+ end
751
+
752
+ it 'returns 404 if the collection is not found' do
753
+ res = @api.post do |req|
754
+ req.url '/servers/localhost/databases/__genghis_spec_test__/collections/fake.files/files'
755
+ req.headers['Content-Type'] = 'application/json'
756
+ req.body = {file: encode_upload('foo')}.to_json
757
+ end
758
+ res.status.should eq 404
759
+ end
760
+
761
+ it 'returns 404 if the collection is not a GridFS files collection' do
762
+ res = @api.post do |req|
763
+ req.url '/servers/localhost/databases/__genghis_spec_test__/collections/test.chunks/files'
764
+ req.headers['Content-Type'] = 'application/json'
765
+ req.body = {file: encode_upload('foo')}.to_json
766
+ end
767
+ res.status.should eq 404
768
+ end
769
+
770
+ it 'returns 404 if the database is not found' do
771
+ res = @api.post do |req|
772
+ req.url '/servers/localhost/databases/__genghis_spec_fake_db__/collections/test.files/files'
773
+ req.headers['Content-Type'] = 'application/json'
774
+ req.body = {file: 'foo'}.to_json
775
+ end
776
+ res.status.should eq 404
777
+ end
778
+ end
779
+
780
+ describe 'GET /servers/:server/databases/:db/collections/:coll/files/:id' do
781
+ it 'returns a document' do
782
+ id = @grid.put('foo')
783
+ res = @api.get do |req|
784
+ req.url '/servers/localhost/databases/__genghis_spec_test__/collections/test.files/files/' + id.to_s
785
+ req.headers.delete('X-Requested-With')
786
+ req.headers.delete('Accept')
787
+ end
788
+
789
+ res.status.should eq 200
790
+ res.headers['Content-Disposition'].should start_with 'attachment'
791
+ res.body.should eq 'foo'
792
+ end
793
+
794
+ it 'returns 404 if the document is not found' do
795
+ res = @api.get '/servers/localhost/databases/__genghis_spec_test__/collections/test.files/files/123'
796
+ res.status.should eq 404
797
+ end
798
+
799
+ it 'returns 404 if the collection is not found' do
800
+ id = @grid.put('bar')
801
+ res = @api.get '/servers/localhost/databases/__genghis_spec_test__/collections/fake.files/files/' + id.to_s
802
+ res.status.should eq 404
803
+ end
804
+
805
+ it 'returns 404 if the collection is not a GridFS files collection' do
806
+ id = @grid.put('baz')
807
+ res = @api.get '/servers/localhost/databases/__genghis_spec_test__/collections/test.chunks/files/' + id.to_s
808
+ res.status.should eq 404
809
+ end
810
+
811
+ it 'returns 404 if the database is not found' do
812
+ id = @grid.put('qux')
813
+ res = @api.get '/servers/localhost/databases/__genghis_spec_fake_db__/collections/test.files/files/' + id.to_s
814
+ res.status.should eq 404
815
+ end
816
+ end
817
+
818
+ describe 'DELETE /servers/:server/databases/:db/collections/:coll/files/:id' do
819
+ it 'deletes a file (and all chunks)' do
820
+ id = @grid.put('wheee')
821
+ res = @api.delete '/servers/localhost/databases/__genghis_spec_test__/collections/test.files/files/' + id.to_s
822
+
823
+ res.status.should eq 200
824
+ res.body.should match_json_expression \
825
+ success: true
826
+
827
+ expect { @grid.get(id) }.to raise_error Mongo::GridFileNotFound
828
+
829
+ # and the chunks should be gone...
830
+ @db['test.chunks'].find(_id: id).count.should eq 0
831
+ end
832
+
833
+ it 'returns 404 if the document is not found' do
834
+ res = @api.delete '/servers/localhost/databases/__genghis_spec_test__/collections/test.files/files/123'
835
+ res.status.should eq 404
836
+ end
837
+
838
+ it 'returns 404 if the collection is not found' do
839
+ id = @grid.put('bar')
840
+ res = @api.delete '/servers/localhost/databases/__genghis_spec_test__/collections/fake.files/files/' + id.to_s
841
+ res.status.should eq 404
842
+ end
843
+
844
+ it 'returns 404 if the collection is not a GridFS files collection' do
845
+ id = @grid.put('baz')
846
+ res = @api.delete '/servers/localhost/databases/__genghis_spec_test__/collections/test.chunks/files/' + id.to_s
847
+ res.status.should eq 404
848
+ end
849
+
850
+ it 'returns 404 if the database is not found' do
851
+ id = @grid.put('qux')
852
+ res = @api.delete '/servers/localhost/databases/__genghis_spec_fake_db__/collections/test.files/files/' + id.to_s
853
+ res.status.should eq 404
854
+ end
855
+ end
856
+ end
663
857
  end
664
858
  end
665
859
  end
data/spec/spec_helper.rb CHANGED
@@ -30,6 +30,10 @@ RSpec.configure do |config|
30
30
  end
31
31
  end
32
32
 
33
+ def encode_upload(file)
34
+ "data:text/plain;base64," + Base64.strict_encode64(file)
35
+ end
36
+
33
37
  config.after :all do
34
38
  # Kill any outstanding Genghis backend
35
39
  Process.kill('HUP', @genghis_pid) unless @genghis_pid.nil?
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: genghisapp
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.6
5
- prerelease:
4
+ version: 2.2.0.beta.1
5
+ prerelease: 6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Justin Hileman
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-24 00:00:00.000000000 Z
12
+ date: 2013-01-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: vegas