ref: master
test/api/articles_test.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 |
require_relative 'test_helper' class ArticlesTest < ActiveSupport::TestCase def setup create_and_activate_user login_api end should 'remove article' do article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") delete "/api/v1/articles/#{article.id}?#{params.to_query}" json = JSON.parse(last_response.body) assert_not_equal 401, last_response.status assert_equal true, json['success'] assert !Article.exists?(article.id) end should 'create uploaded file type article' do params['article'] = { name: 'UploadedFileArticle', type: 'UploadedFile', uploaded_data: {path: '/files/rails.png'} } ActionDispatch::Http::UploadedFile.expects(:new).with({'path' => '/files/rails.png'}).returns(fixture_file_upload('/files/rails.png', 'image/png')) post "/api/v1/profiles/#{person.id}/articles?#{params.to_query}" json = JSON.parse(last_response.body) assert json['public_filename'].present? assert Article.find(json['id']).present? end should 'not remove article without permission' do otherPerson = fast_create(Person, :name => "Other Person") article = fast_create(Article, :profile_id => otherPerson.id, :name => "Some thing") delete "/api/v1/articles/#{article.id}?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal 403, last_response.status assert Article.exists?(article.id) end should 'list articles' do article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") get "/api/v1/articles/?#{params.to_query}" json = JSON.parse(last_response.body) assert_includes json.map { |a| a["id"] }, article.id end should 'list all text articles' do profile = Community.create(identifier: 'my-community', name: 'name-my-community') a1 = fast_create(TextArticle, :profile_id => profile.id) a2 = fast_create(TextArticle, :profile_id => profile.id) a3 = fast_create(TextArticle, :profile_id => profile.id) params['content_type']='TextArticle' get "api/v1/communities/#{profile.id}/articles?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal 3, json.count end should 'get profile homepage' do article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") person.home_page=article person.save! get "/api/v1/profiles/#{person.id}/home_page?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal article.id, json["id"] end should 'not list forbidden article when listing articles' do person = fast_create(Person) article = fast_create(Article, :profile_id => person.id, :name => "Some thing", :published => false) assert !article.published? get "/api/v1/articles?#{params.to_query}" json = JSON.parse(last_response.body) assert_not_includes json.map {|a| a['id']}, article.id end should 'return article by id' do article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") get "/api/v1/articles/#{article.id}?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal article.id, json["id"] end should 'not return article if user has no permission to view it' do person = fast_create(Person, :environment_id => environment.id) article = fast_create(Article, :profile_id => person.id, :name => "Some thing", :published => false) assert !article.published? get "/api/v1/articles/#{article.id}?#{params.to_query}" assert_equal 403, last_response.status end should 'follow a article identified by id' do article = fast_create(Article, :profile_id => @person.id, :name => "Some thing") post "/api/v1/articles/#{article.id}/follow?#{params.to_query}" json = JSON.parse(last_response.body) assert_not_equal 401, last_response.status assert_equal true, json['success'] end should 'return the followers count of an article' do article = fast_create(Article, :profile_id => @person.id, :name => "Some thing") article.person_followers << @person get "/api/v1/articles/#{article.id}?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal 200, last_response.status assert_equal 1, json['followers_count'] end should 'list articles followed by me' do article1 = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") fast_create(Article, :profile_id => user.person.id, :name => "Some other thing") article1.person_followers << @person get "/api/v1/articles/followed_by_me?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal [article1.id], json.map { |a| a['id'] } end should 'list article children' do article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") child1 = fast_create(Article, :parent_id => article.id, :profile_id => user.person.id, :name => "Some thing") child2 = fast_create(Article, :parent_id => article.id, :profile_id => user.person.id, :name => "Some thing") get "/api/v1/articles/#{article.id}/children?#{params.to_query}" json = JSON.parse(last_response.body) assert_equivalent [child1.id, child2.id], json.map { |a| a["id"] } end should 'list all text articles of children' do article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") child1 = fast_create(TextArticle, :parent_id => article.id, :profile_id => user.person.id, :name => "Some thing 1") child2 = fast_create(TextArticle, :parent_id => article.id, :profile_id => user.person.id, :name => "Some thing 2") child3 = fast_create(TextArticle, :parent_id => article.id, :profile_id => user.person.id, :name => "Some thing 3") get "/api/v1/articles/#{article.id}/children?#{params.to_query}" json = JSON.parse(last_response.body) assert_equivalent [child1.id, child2.id, child3.id], json.map { |a| a["id"] } end should 'list public article children for not logged in access' do article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") child1 = fast_create(Article, :parent_id => article.id, :profile_id => user.person.id, :name => "Some thing") child2 = fast_create(Article, :parent_id => article.id, :profile_id => user.person.id, :name => "Some thing") get "/api/v1/articles/#{article.id}/children" json = JSON.parse(last_response.body) assert_equivalent [child1.id, child2.id], json.map { |a| a["id"] } end should 'not list children of forbidden article' do person = fast_create(Person, :environment_id => environment.id) article = fast_create(Article, :profile_id => person.id, :name => "Some thing", :published => false) child1 = fast_create(Article, :parent_id => article.id, :profile_id => person.id, :name => "Some thing") child2 = fast_create(Article, :parent_id => article.id, :profile_id => person.id, :name => "Some thing") get "/api/v1/articles/#{article.id}/children?#{params.to_query}" assert_equal 403, last_response.status end should 'not return child of forbidden article' do person = fast_create(Person, :environment_id => environment.id) article = fast_create(Article, :profile_id => person.id, :name => "Some thing", :published => false) child = fast_create(Article, :parent_id => article.id, :profile_id => person.id, :name => "Some thing") get "/api/v1/articles/#{article.id}/children/#{child.id}?#{params.to_query}" assert_equal 403, last_response.status end should 'not return private child' do person = fast_create(Person, :environment_id => environment.id) article = fast_create(Article, :profile_id => person.id, :name => "Some thing") child = fast_create(Article, :parent_id => article.id, :profile_id => person.id, :name => "Some thing", :published => false) get "/api/v1/articles/#{article.id}/children/#{child.id}?#{params.to_query}" assert_equal 403, last_response.status end should 'not list private child' do person = fast_create(Person, :environment_id => environment.id) article = fast_create(Article, :profile_id => person.id, :name => "Some thing") child = fast_create(Article, :parent_id => article.id, :profile_id => person.id, :name => "Some thing", :published => false) get "/api/v1/articles/#{article.id}/children?#{params.to_query}" json = JSON.parse(last_response.body) assert_not_includes json.map {|a| a['id']}, child.id end should 'perform a vote in a article identified by id' do article = fast_create(Article, :profile_id => @person.id, :name => "Some thing") @params[:value] = 1 post "/api/v1/articles/#{article.id}/vote?#{params.to_query}" json = JSON.parse(last_response.body) assert_not_equal 401, last_response.status assert_equal true, json['vote'] end expose_attributes = %w(id body abstract created_at title author profile categories image votes_for votes_against setting position hits start_date end_date tag_list parent children children_count) expose_attributes.each do |attr| should "expose article #{attr} attribute by default" do article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") get "/api/v1/articles/?#{params.to_query}" json = JSON.parse(last_response.body) assert json.last.has_key?(attr) end end should 'not perform a vote twice in same article' do article = fast_create(Article, :profile_id => @person.id, :name => "Some thing") @params[:value] = 1 ## Perform a vote twice in API should compute only one vote post "/api/v1/articles/#{article.id}/vote?#{params.to_query}" post "/api/v1/articles/#{article.id}/vote?#{params.to_query}" total = article.votes_total assert_equal 1, total end should 'not perform a vote in favor and against a proposal' do article = fast_create(Article, :profile_id => @person.id, :name => "Some thing") @params[:value] = 1 ## Perform a vote in favor a proposal post "/api/v1/articles/#{article.id}/vote?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal 201, last_response.status ## Perform a vote against a proposal @params[:value] = -1 post "/api/v1/articles/#{article.id}/vote?#{params.to_query}" json = JSON.parse(last_response.body) ## The api should not allow to save this vote assert_equal Api::Status::UNPROCESSABLE_ENTITY, last_response.status end should 'not perform a vote in a archived article' do article = fast_create(Article, :profile_id => @person.id, :name => "Some thing", :archived => true) @params[:value] = 1 post "/api/v1/articles/#{article.id}/vote?#{params.to_query}" assert_equal Api::Status::UNPROCESSABLE_ENTITY, last_response.status end should 'not update hit attribute of a specific child if a article is archived' do folder = fast_create(Folder, :profile_id => user.person.id, :archived => true) article = fast_create(Article, :parent_id => folder.id, :profile_id => user.person.id) get "/api/v1/articles/#{folder.id}/children/#{article.id}?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal 0, json['hits'] end should 'find archived articles' do article1 = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") article2 = fast_create(Article, :profile_id => user.person.id, :name => "Some thing", :archived => true) params[:archived] = true get "/api/v1/articles/?#{params.to_query}" json = JSON.parse(last_response.body) assert_not_includes json.map { |a| a["id"] }, article1.id assert_includes json.map { |a| a["id"] }, article2.id end should "update body of article created by me" do new_value = "Another body" params[:article] = {:body => new_value} article = fast_create(Article, :profile_id => person.id) post "/api/v1/articles/#{article.id}?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal new_value, json["body"] end should "update title of article created by me" do new_value = "Another name" params[:article] = {:name => new_value} article = fast_create(Article, :profile_id => person.id) post "/api/v1/articles/#{article.id}?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal new_value, json["title"] end should 'not update article of another user' do another_person = fast_create(Person, :environment_id => environment.id) article = fast_create(Article, :profile_id => another_person.id) params[:article] = {:title => 'Some title'} post "/api/v1/articles/#{article.id}?#{params.to_query}" assert_equal 403, last_response.status end should 'not update article without permission in community' do community = fast_create(Community, :environment_id => environment.id) article = fast_create(Article, :profile_id => community.id) params[:article] = {:name => 'New title'} post "/api/v1/articles/#{article.id}?#{params.to_query}" assert_equal 403, last_response.status end should 'update article of community if user has permission' do community = fast_create(Community, :environment_id => environment.id) give_permission(person, 'post_content', community) article = fast_create(Article, :profile_id => community.id) new_value = "Another body" params[:article] = {:body => new_value} post "/api/v1/articles/#{article.id}?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal new_value, json["body"] end should 'list articles with pagination' do Article.destroy_all article_one = fast_create(Article, :profile_id => user.person.id, :name => "Another thing", :created_at => 2.days.ago) article_two = fast_create(Article, :profile_id => user.person.id, :name => "Some thing", :created_at => 1.day.ago) params[:page] = 1 params[:per_page] = 1 get "/api/v1/articles/?#{params.to_query}" json_page_one = JSON.parse(last_response.body) params[:page] = 2 params[:per_page] = 1 get "/api/v1/articles/?#{params.to_query}" json_page_two = JSON.parse(last_response.body) assert_includes json_page_one.map { |a| a["id"] }, article_two.id assert_not_includes json_page_one.map { |a| a["id"] }, article_one.id assert_includes json_page_two.map { |a| a["id"] }, article_one.id assert_not_includes json_page_two.map { |a| a["id"] }, article_two.id end should 'list articles with timestamp' do article_one = fast_create(Article, :profile_id => user.person.id, :name => "Another thing") article_two = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") article_one.updated_at = Time.now.in_time_zone + 3.hours article_one.save! params[:timestamp] = Time.now.in_time_zone + 1.hours get "/api/v1/articles/?#{params.to_query}" json = JSON.parse(last_response.body) assert_includes json.map { |a| a["id"] }, article_one.id assert_not_includes json.map { |a| a["id"] }, article_two.id end ############################# # Profile Articles # ############################# profile_kinds = %w(community person enterprise) profile_kinds.each do |kind| should "return article by #{kind}" do profile = fast_create(kind.camelcase.constantize, :environment_id => environment.id) article = fast_create(Article, :profile_id => profile.id, :name => "Some thing") get "/api/v1/#{kind.pluralize}/#{profile.id}/articles/#{article.id}?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal article.id, json["id"] end should "not return article by #{kind} if user has no permission to view it" do profile = fast_create(kind.camelcase.constantize, :environment_id => environment.id) article = fast_create(Article, :profile_id => profile.id, :name => "Some thing", :published => false) assert !article.published? get "/api/v1/#{kind.pluralize}/#{profile.id}/articles/#{article.id}?#{params.to_query}" assert_equal 403, last_response.status end should "not list forbidden article when listing articles by #{kind}" do profile = fast_create(kind.camelcase.constantize, :environment_id => environment.id) article = fast_create(Article, :profile_id => profile.id, :name => "Some thing", :published => false) assert !article.published? get "/api/v1/#{kind.pluralize}/#{profile.id}/articles?#{params.to_query}" json = JSON.parse(last_response.body) assert_not_includes json.map {|a| a['id']}, article.id end should "return article by #{kind} and path" do profile = fast_create(kind.camelcase.constantize, :environment_id => environment.id) parent_article = Folder.create!(:profile => profile, :name => "Parent Folder") article = Article.create!(:profile => profile, :name => "Some thing", :parent => parent_article) params[:path] = parent_article.slug+'/'+article.slug get "/api/v1/#{kind.pluralize}/#{profile.id}/articles?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal article.id, json["id"] end should "return an empty array if theres id no article in path of #{kind}" do profile = fast_create(kind.camelcase.constantize, :environment_id => environment.id) parent_article = Folder.create!(:profile => profile, :name => "Parent Folder") article = Article.create!(:profile => profile, :name => "Some thing", :parent => parent_article) params[:path] = 'no-path' get "/api/v1/#{kind.pluralize}/#{profile.id}/articles?#{params.to_query}" json = JSON.parse(last_response.body) assert json.empty? end should "not return article by #{kind} and path if user has no permission to view it" do profile = fast_create(kind.camelcase.constantize, :environment_id => environment.id) parent_article = Folder.create!(:profile => profile, :name => "Parent Folder") article = Article.create!(:profile => profile, :name => "Some thing", :parent => parent_article, :published => false) assert !article.published? params[:path] = parent_article.slug+'/'+article.slug get "/api/v1/#{kind.pluralize}/#{profile.id}/articles?#{params.to_query}" assert_equal 403, last_response.status end should "get article in #{kind} by path in articles endpoint be deprecated" do profile = fast_create(kind.camelcase.constantize, :environment_id => environment.id) parent_article = Folder.create!(:profile => profile, :name => "Parent Folder") article = Article.create!(:profile => profile, :name => "Some thing", :parent => parent_article) params[:path] = parent_article.slug+'/'+article.slug get "/api/v1/#{kind.pluralize}/#{profile.id}/articles?#{params.to_query}" assert_equal Api::Status::DEPRECATED, last_response.status end should "return article by #{kind} and path with key parameter" do profile = fast_create(kind.camelcase.constantize, :environment_id => environment.id) folder = Folder.create!(:profile => profile, :name => "Folder") parent_article = Folder.create!(:profile => profile, :name => "Parent Folder", :parent => folder) article = Article.create!(:profile => profile, :name => "Some thing", :parent => parent_article) path = folder.slug + '/' + parent_article.slug+'/'+article.slug params[:key] = 'path' get "/api/v1/#{kind.pluralize}/#{profile.id}/articles/#{path}?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal article.id, json["id"] end end ############################# # Group Profile Articles # ############################# group_kinds = %w(community enterprise) group_kinds.each do |kind| should "#{kind}: create article" do profile = fast_create(kind.camelcase.constantize, :environment_id => environment.id) give_permission(user.person, 'post_content', profile) params[:article] = {:name => "Title"} post "/api/v1/#{kind.pluralize}/#{profile.id}/articles?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal "Title", json["title"] end should "#{kind}: do not create article if user has no permission to post content" do profile = fast_create(kind.camelcase.constantize, :environment_id => environment.id) give_permission(user.person, 'invite_members', profile) params[:article] = {:name => "Title"} post "/api/v1/#{kind.pluralize}/#{profile.id}/articles?#{params.to_query}" assert_equal 403, last_response.status end should "#{kind} create article with parent" do profile = fast_create(kind.camelcase.constantize, :environment_id => environment.id) Person.any_instance.stubs(:can_post_content?).with(profile).returns(true) article = fast_create(Article) params[:article] = {:name => "Title", :parent_id => article.id} post "/api/v1/#{kind.pluralize}/#{profile.id}/articles?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal article.id, json["parent"]["id"] end should "#{kind} create article with content type passed as parameter" do profile = fast_create(kind.camelcase.constantize, :environment_id => environment.id) Person.any_instance.stubs(:can_post_content?).with(profile).returns(true) Article.delete_all params[:article] = {:name => "Title"} params[:content_type] = 'TextArticle' post "/api/v1/#{kind.pluralize}/#{profile.id}/articles?#{params.to_query}" json = JSON.parse(last_response.body) assert_kind_of TextArticle, Article.last end should "#{kind} create article with type passed as parameter" do profile = fast_create(kind.camelcase.constantize, :environment_id => environment.id) Person.any_instance.stubs(:can_post_content?).with(profile).returns(true) Article.delete_all params[:article] = {:name => "Title", :type => 'TextArticle'} post "/api/v1/#{kind.pluralize}/#{profile.id}/articles?#{params.to_query}" json = JSON.parse(last_response.body) assert_kind_of TextArticle, Article.last end should "#{kind}: create article of TexrArticle type if no content type is passed as parameter" do profile = fast_create(kind.camelcase.constantize, :environment_id => environment.id) Person.any_instance.stubs(:can_post_content?).with(profile).returns(true) params[:article] = {:name => "Title"} post "/api/v1/#{kind.pluralize}/#{profile.id}/articles?#{params.to_query}" json = JSON.parse(last_response.body) assert_kind_of TextArticle, Article.last end should "#{kind}: not create article with invalid article content type" do profile = fast_create(kind.camelcase.constantize, :environment_id => environment.id) profile.add_member(user.person) params[:article] = {:name => "Title"} params[:content_type] = 'Person' post "/api/v1/#{kind.pluralize}/#{profile.id}/articles?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal 403, last_response.status end should "#{kind} create article defining the correct profile" do profile = fast_create(kind.camelcase.constantize, :environment_id => environment.id) Person.any_instance.stubs(:can_post_content?).with(profile).returns(true) params[:article] = {:name => "Title"} post "/api/v1/#{kind.pluralize}/#{profile.id}/articles?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal profile.id, json['profile']['id'] end should "#{kind}: create article defining the created_by" do profile = fast_create(kind.camelcase.constantize, :environment_id => environment.id) Person.any_instance.stubs(:can_post_content?).with(profile).returns(true) params[:article] = {:name => "Title"} post "/api/v1/#{kind.pluralize}/#{profile.id}/articles?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal user.person, Article.last.created_by end should "#{kind}: create article defining the last_changed_by" do profile = fast_create(kind.camelcase.constantize, :environment_id => environment.id) Person.any_instance.stubs(:can_post_content?).with(profile).returns(true) params[:article] = {:name => "Title"} post "/api/v1/#{kind.pluralize}/#{profile.id}/articles?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal user.person, Article.last.last_changed_by end end ############################# # Person Articles # ############################# should 'create article in a person' do params[:article] = {:name => "Title"} post "/api/v1/people/#{user.person.id}/articles?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal "Title", json["title"] end should 'person do not create article if user has no permission to post content' do person = fast_create(Person, :environment_id => environment.id) params[:article] = {:name => "Title"} post "/api/v1/people/#{person.id}/articles?#{params.to_query}" assert_equal 403, last_response.status end should 'person create article with parent' do article = fast_create(Article) params[:article] = {:name => "Title", :parent_id => article.id} post "/api/v1/people/#{user.person.id}/articles?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal article.id, json["parent"]["id"] end should 'person create article with content type passed as parameter' do Article.delete_all params[:article] = {:name => "Title"} params[:content_type] = 'TextArticle' post "/api/v1/people/#{user.person.id}/articles?#{params.to_query}" json = JSON.parse(last_response.body) assert_kind_of TextArticle, Article.last end should 'person create article of TextArticle type if no content type is passed as parameter' do params[:article] = {:name => "Title"} post "/api/v1/people/#{user.person.id}/articles?#{params.to_query}" json = JSON.parse(last_response.body) assert_kind_of TextArticle, Article.last end should 'person not create article with invalid article content type' do params[:article] = {:name => "Title"} params[:content_type] = 'Person' post "/api/v1/people/#{user.person.id}/articles?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal 403, last_response.status end should 'person create article defining the correct profile' do params[:article] = {:name => "Title"} post "/api/v1/people/#{user.person.id}/articles?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal user.person, Article.last.profile end should 'person create article defining the created_by' do params[:article] = {:name => "Title"} post "/api/v1/people/#{user.person.id}/articles?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal user.person, Article.last.created_by end should 'person create article defining the last_changed_by' do params[:article] = {:name => "Title"} post "/api/v1/people/#{user.person.id}/articles?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal user.person, Article.last.last_changed_by end should 'list article children with partial fields' do article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") child1 = fast_create(Article, :parent_id => article.id, :profile_id => user.person.id, :name => "Some thing") params[:fields] = [:title] get "/api/v1/articles/#{article.id}/children?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal ['title'], json.first.keys end should "create article child" do article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") params[:article] = {:name => "Title"} post "/api/v1/articles/#{article.id}/children?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal article.id, json["parent"]["id"] end should "do not create article child if user has no permission to post content" do profile = fast_create(Profile, :environment_id => environment.id) article = fast_create(Article, :profile_id => profile.id, :name => "Some thing") give_permission(user.person, 'invite_members', profile) params[:article] = {:name => "Title"} post "/api/v1/articles/#{article.id}/children?#{params.to_query}" assert_equal 403, last_response.status end should 'suggest article children' do article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") params[:target_id] = user.person.id params[:article] = {:name => "Article name", :body => "Article body"} assert_difference "SuggestArticle.count" do post "/api/v1/articles/#{article.id}/children/suggest?#{params.to_query}" end json = JSON.parse(last_response.body) assert_equal 'SuggestArticle', json['type'] end should 'suggest event children' do article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") params[:target_id] = user.person.id params[:article] = {:name => "Article name", :body => "Article body", :type => "Event"} assert_difference "SuggestArticle.count" do post "/api/v1/articles/#{article.id}/children/suggest?#{params.to_query}" end json = JSON.parse(last_response.body) assert_equal 'SuggestArticle', json['type'] end should 'update hit attribute of article children' do a1 = fast_create(Article, :profile_id => user.person.id) a2 = fast_create(Article, :parent_id => a1.id, :profile_id => user.person.id) a3 = fast_create(Article, :parent_id => a1.id, :profile_id => user.person.id) get "/api/v1/articles/#{a1.id}/children?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal [1, 1], json.map { |a| a['hits']} assert_equal [0, 1, 1], [a1.reload.hits, a2.reload.hits, a3.reload.hits] end should 'update hit attribute of article specific children' do a1 = fast_create(Article, :profile_id => user.person.id) a2 = fast_create(Article, :parent_id => a1.id, :profile_id => user.person.id) get "/api/v1/articles/#{a1.id}/children/#{a2.id}?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal 1, json['hits'] end should 'list all events of a community in a given category' do co = Community.create(identifier: 'my-community', name: 'name-my-community') c1 = Category.create(environment: Environment.default, name: 'my-category') c2 = Category.create(environment: Environment.default, name: 'dont-show-me-this-category') e1 = fast_create(Event, :profile_id => co.id) e2 = fast_create(Event, :profile_id => co.id) e1.categories << c1 e2.categories << c2 e1.save! e2.save! params['content_type']='Event' get "api/v1/communities/#{co.id}/articles?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal json.count, 2 end should 'list a event of a community in a given category' do co = Community.create(identifier: 'my-community', name: 'name-my-community') c1 = Category.create(environment: Environment.default, name: 'my-category') c2 = Category.create(environment: Environment.default, name: 'dont-show-me-this-category') e1 = fast_create(Event, :profile_id => co.id) e2 = fast_create(Event, :profile_id => co.id) e1.categories << c1 e2.categories << c2 e1.save! e2.save! params['category_ids[]']=c1.id params['content_type']='Event' get "api/v1/communities/#{co.id}/articles?#{params.to_query}" json = JSON.parse(last_response.body) #should show only one article, since the other not in the same category assert_equal 1, json.count assert_equal e1.id, json[0]['id'] end should 'not list uncategorized event of a community if a category is given' do co = Community.create(identifier: 'my-community', name: 'name-my-community') c1 = Category.create(environment: Environment.default, name: 'my-category') c2 = Category.create(environment: Environment.default, name: 'dont-show-me-this-category') e1 = fast_create(Event, :profile_id => co.id) e2 = fast_create(Event, :profile_id => co.id) e3 = fast_create(Event, :profile_id => co.id) e1.categories << c1 e2.categories << c2 params['category_ids[]']=c1.id params['content_type']='Event' get "api/v1/communities/#{co.id}/articles?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal 1, json.count assert_equal e1.id, json[0]['id'] end should 'list events of a community in a given 2 categories' do co = Community.create(identifier: 'my-community', name: 'name-my-community') c1 = Category.create(environment: Environment.default, name: 'my-category') c2 = Category.create(environment: Environment.default, name: 'dont-show-me-this-category') e1 = fast_create(Event, :profile_id => co.id) e2 = fast_create(Event, :profile_id => co.id) e1.categories << c1 e2.categories << c2 e1.save! e2.save! params['content_type']='Event' params['categories_ids'] = [c1.id, c2.id] get "api/v1/communities/#{co.id}/articles?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal json.count, 2 end should 'Show 2 events since it uses an IN operator for category instead of an OR' do co = Community.create(identifier: 'my-community', name: 'name-my-community') c1 = Category.create(environment: Environment.default, name: 'my-category') c2 = Category.create(environment: Environment.default, name: 'dont-show-me-this-category') c3 = Category.create(environment: Environment.default, name: 'extra-category') e1 = fast_create(Event, :profile_id => co.id) e2 = fast_create(Event, :profile_id => co.id) e1.categories << c1 e2.categories << c2 e1.save! e2.save! params['content_type']='Event' params['categories_ids'] = [c1.id, c2.id, c3.id] get "api/v1/communities/#{co.id}/articles?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal json.count, 2 end ARTICLE_ATTRIBUTES = %w(followers_count votes_count comments_count) ARTICLE_ATTRIBUTES.map do |attribute| define_method "test_should_expose_#{attribute}_attribute_in_article_enpoints" do article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") get "/api/v1/articles/#{article.id}?#{params.to_query}" json = JSON.parse(last_response.body) assert_not_nil json[attribute] end end should 'only show article comments when optional_fields comments is present' do person = fast_create(Person) article = fast_create(Article, :profile_id => person.id, :name => "Some thing") article.comments.create!(:body => "another comment", :author => person) get "/api/v1/articles/#{article.id}/?#{params.merge(:optional_fields => [:comments]).to_query}" json = JSON.parse(last_response.body) assert_includes json.keys, "comments" assert_equal json["comments"].first["body"], "another comment" get "/api/v1/articles/#{article.id}/?#{params.to_query}" json = JSON.parse(last_response.body) assert_not_includes json.keys, "comments" end should 'not list private child when get the parent article' do person = fast_create(Person, :environment_id => environment.id) article = fast_create(Article, :profile_id => person.id, :name => "Some thing") child = fast_create(Article, :parent_id => article.id, :profile_id => person.id, :name => "Some thing", :published => false) get "/api/v1/articles/#{article.id}?#{params.to_query}" json = JSON.parse(last_response.body) assert_not_includes json['children'].map {|a| a['id']}, child.id end should 'list article permissions when get an article' do community = fast_create(Community) give_permission(person, 'post_content', community) article = fast_create(Article, :profile_id => community.id) get "/api/v1/articles/#{article.id}?#{params.to_query}" json = JSON.parse(last_response.body) assert_includes json["permissions"], 'allow_post_content' end should 'return only article fields defined in parameter' do Article.destroy_all article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") params[:fields] = {:only => ['id', 'title']} get "/api/v1/articles/?#{params.to_query}" json = JSON.parse(last_response.body) assert_equivalent ['id', 'title'], json.first.keys end should 'return all article fields except the ones defined in parameter' do Article.destroy_all article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") params[:fields] = {:except => ['id', 'title']} get "/api/v1/articles/?#{params.to_query}" json = JSON.parse(last_response.body) assert_not_includes json.first.keys, 'id' assert_not_includes json.first.keys, 'title' end should 'search for articles' do article1 = fast_create(Article, profile_id: user.person.id, name: "Some thing") article2 = fast_create(Article, profile_id: user.person.id, name: "Other thing") params[:search] = 'some' get "/api/v1/articles/?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal [article1.id], json.map { |a| a["id"] } end should 'search for articles of different types' do article1 = fast_create(Event, profile_id: user.person.id, name: "Some thing") article2 = fast_create(TextArticle, profile_id: user.person.id, name: "Some other thing") article3 = fast_create(Article, profile_id: user.person.id, name: "Other thing") params[:search] = 'some' get "/api/v1/articles/?#{params.to_query}" json = JSON.parse(last_response.body) assert_equivalent [article1.id, article2.id], json.map { |a| a["id"] } end should "match error messages" do profile = fast_create(Community, :environment_id => environment.id) give_permission(user.person, 'post_content', profile) params[:article] = {:name => ""} post "/api/v1/communities/#{profile.id}/articles?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal ({"name" => [{"error"=>"blank", "full_message"=>"Title can't be blank"}]}), json["errors"] end end |