colab_integration_plugin_controller_test.rb 2.81 KB
require File.dirname(__FILE__) + '/../../../../test/test_helper'
require File.dirname(__FILE__) + '/../../controllers/colab_integration_plugin_controller'

class ColabIntegrationPluginController; def rescue_action(e) raise e end; end

class ColabIntegrationPluginControllerTest < ActionController::TestCase

  include ProfileDataExport

  def setup
    @controller = ColabIntegrationPluginController.new
    @john = fast_create(Person, :name => "John Snow")
    @arya = fast_create(Person, :name => "Arya Stark")
    @joffrey = fast_create(Person, :name => "Joffrey Lannister")

    @stark = fast_create(Community, :name => "House Stark")
    @stark.add_member @john
    @stark.add_admin @arya

    @article = create(TinyMceArticle, :name => "The Night's Watch Oath", :body => "Night gathers, and now my watch begins...", :profile => @john)

    @game_of_thrones = fast_create(Community, :name => "Game of Thrones")
    @game_of_thrones.add_member @john
    @game_of_thrones.add_member @arya
    @game_of_thrones.add_member @joffrey
  end

  should "return a json with all information from the environment" do
    get :index

    environment_data = JSON.parse(@response.body)
    assert_equal environment_data["total"], 5

    persons = environment_data["profiles"].select{ |profile|
      profile["type"] == "Person"
    }
    assert_equal persons.count, 3

    communities = environment_data["profiles"].select{ |profile|
      profile["type"] == "Community"
    }
    assert_equal communities.count, 2

    persons_with_article = persons.select{ |person|
      person["articles-count"] > 0
    }
    assert_equal persons_with_article.count, 1
    assert_nothing_raised do
      Time.parse environment_data["timestamp"]
    end
  end

  should "return a json with only the last updated profiles" do
    time = Time.at 0

    Profile.destroy_all

    kingsland = Community.create!(:name => "Kings Land", :identifier => "kings-land")
    winterfell = Community.create!(:name => "Winterfell", :identifier => "winterfell")

    get :index, :timestamp => time.to_s

    environment_data = JSON.parse(@response.body)

    assert_equal environment_data["total"], 2

    winterfell.updated_at = time
    winterfell.save!

    get :index, :timestamp => time.to_s

    environment_data = JSON.parse(@response.body)

    assert_equal environment_data["total"], 1
  end

  should "return a hash with activities of the user" do
    person = fast_create(Person, :name => "User")
    fast_create(Community, :name => "User Community").add_member person
    fast_create(Community, :name => "User Community 2").add_member person

    attrs = attr_to_hash(person)

    assert_equal 2, attrs['activities-count']
    assert attrs['activities'].first.has_key?("verb")
    assert attrs['activities'].first.has_key?("params")
    assert attrs['activities'].first["verb"] == "join_community"
  end
end