From 5e0ff19955cb131dee196d756d864ac557877754 Mon Sep 17 00:00:00 2001 From: Caio SBA Date: Mon, 16 May 2016 09:49:49 -0300 Subject: [PATCH] Ticket #45: Moving the environment tags API to the api/tags.rb file, fixing authentication and implementing tests for the tags API --- lib/noosfero/api/v1/environments.rb | 5 ----- lib/noosfero/api/v1/tags.rb | 12 ++++++++---- test/api/environment_test.rb | 12 ------------ test/api/tags_test.rb | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 21 deletions(-) create mode 100644 test/api/tags_test.rb diff --git a/lib/noosfero/api/v1/environments.rb b/lib/noosfero/api/v1/environments.rb index dc67f9e..a030430 100644 --- a/lib/noosfero/api/v1/environments.rb +++ b/lib/noosfero/api/v1/environments.rb @@ -10,11 +10,6 @@ module Noosfero present environment.signup_person_fields end - desc 'Return the tag counts for this environment' - get '/tags' do - present environment.tag_counts - end - get ':id' do local_environment = nil if (params[:id] == "default") diff --git a/lib/noosfero/api/v1/tags.rb b/lib/noosfero/api/v1/tags.rb index 3e902a1..4529a55 100644 --- a/lib/noosfero/api/v1/tags.rb +++ b/lib/noosfero/api/v1/tags.rb @@ -2,12 +2,8 @@ module Noosfero module API module V1 class Tags < Grape::API - before { authenticate! } - resource :articles do - resource ':id/tags' do - get do article = find_article(environment.articles, params[:id]) present article.tag_list @@ -15,6 +11,7 @@ module Noosfero desc "Add a tag to an article" post do + authenticate! article = find_article(environment.articles, params[:id]) article.tag_list=params[:tags] article.save @@ -22,6 +19,13 @@ module Noosfero end end end + + resource :environment do + desc 'Return the tag counts for this environment' + get '/tags' do + present environment.tag_counts + end + end end end end diff --git a/test/api/environment_test.rb b/test/api/environment_test.rb index 020dc64..287e2f0 100644 --- a/test/api/environment_test.rb +++ b/test/api/environment_test.rb @@ -67,16 +67,4 @@ class EnvironmentTest < ActiveSupport::TestCase json = JSON.parse(last_response.body) assert_equal context_env.id, json['id'] end - - should 'return number of tags' do - person = fast_create(Person) - person.articles.create!(:name => 'article 1', :tag_list => 'first-tag') - person.articles.create!(:name => 'article 2', :tag_list => 'first-tag, second-tag') - person.articles.create!(:name => 'article 3', :tag_list => 'first-tag, second-tag, third-tag') - - get '/api/v1/environment/tags' - json = JSON.parse(last_response.body) - assert_equal({ 'first-tag' => 3, 'second-tag' => 2, 'third-tag' => 1 }, json) - end - end diff --git a/test/api/tags_test.rb b/test/api/tags_test.rb new file mode 100644 index 0000000..bcc1dc3 --- /dev/null +++ b/test/api/tags_test.rb @@ -0,0 +1,48 @@ +require_relative 'test_helper' + +class TagsTest < ActiveSupport::TestCase + + def setup + create_and_activate_user + end + + should 'get article tags' do + profile = fast_create(Profile) + a = profile.articles.create(name: 'Test') + a.tags.create! name: 'foo' + + get "/api/v1/articles/#{a.id}/tags?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_equal ['foo'], json + end + + should 'post article tags' do + login_api + profile = fast_create(Profile) + a = profile.articles.create(name: 'Test') + + post "/api/v1/articles/#{a.id}/tags?#{params.to_query}&tags=foo" + assert_equal 201, last_response.status + assert_equal ['foo'], a.reload.tag_list + end + + should 'not post article tags if not authenticated' do + profile = fast_create(Profile) + a = profile.articles.create(name: 'Test') + + post "/api/v1/articles/#{a.id}/tags?#{params.to_query}&tags=foo" + assert_equal 401, last_response.status + assert_equal [], a.reload.tag_list + end + + should 'get environment tags' do + person = fast_create(Person) + person.articles.create!(:name => 'article 1', :tag_list => 'first-tag') + person.articles.create!(:name => 'article 2', :tag_list => 'first-tag, second-tag') + person.articles.create!(:name => 'article 3', :tag_list => 'first-tag, second-tag, third-tag') + + get '/api/v1/environment/tags' + json = JSON.parse(last_response.body) + assert_equal({ 'first-tag' => 3, 'second-tag' => 2, 'third-tag' => 1 }, json) + end +end -- libgit2 0.21.2