[Git][noosfero/noosfero][master] 5 commits: Ticket #45: Adding an API endpoint to get tags from environment
Leandro Nunes
gitlab at mg.gitlab.com
Mon May 16 10:20:34 BRT 2016
Leandro Nunes pushed to branch master at Noosfero / noosfero
Commits:
dfe77cb8 by Caio SBA at 2016-05-13T12:38:00-03:00
Ticket #45: Adding an API endpoint to get tags from environment
- - - - -
5e0ff199 by Caio SBA at 2016-05-16T09:49:49-03:00
Ticket #45: Moving the environment tags API to the api/tags.rb file, fixing authentication and implementing tests for the tags API
- - - - -
9cc1bb44 by Caio SBA at 2016-05-16T09:55:58-03:00
Fixing conflict
- - - - -
8898935c by Caio SBA at 2016-05-16T09:59:22-03:00
Ticket #45: Adding APi endpoint to get environment tags
- - - - -
54bc457e by Leandro Nunes at 2016-05-16T13:19:56+00:00
Merge branch 'tags-api' into 'master'
Improvements to tags API
* Get environment tags
* Require authentication only to post article tags
* Tests for the whole tags API
See merge request !918
- - - - -
3 changed files:
- app/api/v1/tags.rb
- test/api/environment_test.rb
- + test/api/tags_test.rb
Changes:
=====================================
app/api/v1/tags.rb
=====================================
--- a/app/api/v1/tags.rb
+++ b/app/api/v1/tags.rb
@@ -1,12 +1,8 @@
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
@@ -14,6 +10,7 @@ module Api
desc "Add a tag to an article"
post do
+ authenticate!
article = find_article(environment.articles, params[:id])
article.tag_list=params[:tags]
article.save
@@ -21,6 +18,13 @@ module Api
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
=====================================
test/api/environment_test.rb
=====================================
--- a/test/api/environment_test.rb
+++ b/test/api/environment_test.rb
@@ -67,5 +67,4 @@ class EnvironmentTest < ActiveSupport::TestCase
json = JSON.parse(last_response.body)
assert_equal context_env.id, json['id']
end
-
end
=====================================
test/api/tags_test.rb
=====================================
--- /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
View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/d8b7fec7e322b98c0dd92356e3f643e7c0363197...54bc457e3042dee50f37913f5709bd91f9592eda
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20160516/41956235/attachment-0001.html>
More information about the Noosfero-dev
mailing list