Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing uppgrading branch #2861

Open
wants to merge 10 commits into
base: upgrading
Choose a base branch
from
118 changes: 116 additions & 2 deletions spec/api_v3_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,128 @@ def create_user(data)

# Confirm captcha token
post api_v3_setup_user_path, params: {
token: captcha_token.token,
code: captcha_token.code
token: captcha_token.token,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dexterhenry why an extra space here

code: captcha_token.code
}, as: :json

# Resolve user
response_data = JSON.parse(response.body)
User.find(response_data['id'])
end

def json_response
@json_response ||= JSON.parse(response.body, symbolize_names: true)
end

def header_token_authorization
@header_token_authorization ||= create_acces_read_app
end

def headers_token_authorization
@header_token_authorization_create ||= create_acces_app
end

def create_acces_app
search_app = ::Setup::Application.where(namespace: 'Test', name: 'ApiV3').first
app = search_app ? search_app : ::Setup::Application.create!(namespace: 'Test', name: 'ApiV3')

access_grant_read = ::Cenit::OauthAccessGrant.create!(
application_id: app.application_id,
scope: 'read'
)

access_grant_create = ::Cenit::OauthAccessGrant.create!(
application_id: app.application_id,
scope: 'create'
)

access_grant_delete = ::Cenit::OauthAccessGrant.create!(
application_id: app.application_id,
scope: 'delete'
)

access_grant_update = ::Cenit::OauthAccessGrant.create!(
application_id: app.application_id,
scope: 'update'
)

access_grant_digest = ::Cenit::OauthAccessGrant.create!(
application_id: app.application_id,
scope: 'digest'
)

test_user = ::User.current
access_read = ::Cenit::OauthAccessToken.for(
app.application_id,
access_grant_read.scope,
test_user
)

access_create = ::Cenit::OauthAccessToken.for(
app.application_id,
access_grant_create.scope,
test_user
)

access_delete = ::Cenit::OauthAccessToken.for(
app.application_id,
access_grant_delete.scope,
test_user
)

access_update = ::Cenit::OauthAccessToken.for(
app.application_id,
access_grant_update.scope,
test_user
)

access_digest = ::Cenit::OauthAccessToken.for(
app.application_id,
access_grant_digest.scope,
test_user
)

return {
header_token_read: {
Authorization: "Bearer #{access_read[:access_token]}"
},
header_token_create: {
Authorization: "Bearer #{access_create[:access_token]}"
},
header_token_delete: {
Authorization: "Bearer #{access_delete[:access_token]}"
},
header_token_update: {
Authorization: "Bearer #{access_update[:access_token]}"
},
header_token_digest: {
Authorization: "Bearer #{access_digest[:access_token]}"
}
}
end

def create_acces_read_app
search_app = ::Setup::Application.where(namespace: 'Testing', name: 'ApiV3Post').first
app = search_app ? search_app : ::Setup::Application.create!(namespace: 'Testing', name: 'ApiV3Post')


access_grant = ::Cenit::OauthAccessGrant.create!(
application_id: app.application_id,
scope: 'read'
)

test_user = ::User.current
access = ::Cenit::OauthAccessToken.for(
app.application_id,
access_grant.scope,
test_user
)

return header_token = {
Authorization: "Bearer #{access[:access_token]}"
}
end

end
end
end
6 changes: 6 additions & 0 deletions spec/factories/connections.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@
name 'Store II'
url 'http://localhost:3002/cenit'
end

factory :stores_connection, class: Setup::Connection do
sequence(:name) { |n| "Store #{n}" }
url 'http://localhost:3002/cenit'
end

end
6 changes: 6 additions & 0 deletions spec/factories/namespaces.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FactoryGirl.define do
factory :namespaces, class: Setup::Namespace do
sequence(:name) { |n| "Namespace#{n}" }
sequence(:slug) { |n| "slug#{n}" }
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'rails_helper'
RSpec.describe Api::V3::ApiController, type: :request do

end
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require 'rails_helper'
RSpec.describe Api::V3::ApiController, type: :request do

describe "POST /api/v3/setup/connection" do
# Create an Connection
it "With authorized token request" do
request_data = {name: 'connection_test', url: "http://localhost:3002/cenit/test"}

# The connection does not exists
connection = Setup::Connection.where(name: request_data[:name]).first
expect(connection).not_to be

# Connection creation request success
post '/api/v3/setup/connection', params: request_data, headers: headers_token_authorization[:header_token_create], as: :json
expect(response).to have_http_status(:ok)
end
end

describe "GET /api/v3/setup/connection/{id}" do
# Retrieve an existing Connection
it "Fail retrieve connection with bad id" do
get "/api/v3/setup/connection/not_id", headers: header_token_authorization
expect(response).to have_http_status(:not_found)
expect(json_response[:status]).to eq("item not found")
end

it "Successful retrieve connection" do
connection = FactoryGirl.create(:stores_connection)
connection_id = connection.id

get "/api/v3/setup/connection/#{connection_id}", headers: header_token_authorization
expect(response).to have_http_status(:ok)
expect(json_response).to include(:id, :name, :url)
expect(json_response[:name]).to eq(connection.name)
expect(json_response[:url]).to eq(connection.url)
end
end
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the last lines


Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'rails_helper'
RSpec.describe Api::V3::ApiController, type: :request do

end
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'rails_helper'
RSpec.describe Api::V3::ApiController, type: :request do

end
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require 'rails_helper'
RSpec.describe Api::V3::ApiController, type: :request do

describe "POST /api/v3/setup/namespace" do
# Create an Namespace
it "With authorized token request" do
request_data = {name: 'namespace_test', slug: "slug_test"}

# The namespace does not exists
namespace = Setup::Namespace.where(name: request_data[:name]).first
expect(namespace).not_to be

# Namespace creation request success
post '/api/v3/setup/namespace', params: request_data, headers: headers_token_authorization[:header_token_create], as: :json
expect(response).to have_http_status(:ok)
end
end

describe "GET /api/v3/setup/namespace/{id}" do
# Retrieve an existing Namespace
it "Fail retrieve namespace with bad id" do
get "/api/v3/setup/namespace/not_id", headers: header_token_authorization
expect(response).to have_http_status(:not_found)
expect(json_response[:status]).to eq("item not found")
end

it "Successful retrieve namespace" do
namespace = FactoryGirl.create(:namespaces)
namespace_id = namespace.id

get "/api/v3/setup/namespace/#{namespace_id}", headers: header_token_authorization
expect(response).to have_http_status(:ok)
expect(json_response).to include(:id, :name, :slug)
expect(json_response[:name]).to eq(namespace.name)
expect(json_response[:slug]).to eq(namespace.slug)
end
end

end

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'rails_helper'
RSpec.describe Api::V3::ApiController, type: :request do

end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove extra space

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'rails_helper'
RSpec.describe Api::V3::ApiController, type: :request do

end
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'rails_helper'
RSpec.describe Api::V3::ApiController, type: :request do

end
49 changes: 49 additions & 0 deletions spec/requests/app/controllers/api/v3/models_index_api_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require 'rails_helper'
RSpec.describe Api::V3::ApiController, type: :request do
Setup::Models.all.each do |model|
model_name_array = model.name.to_s.split('::')
namespace = model_name_array.first.downcase
underscore_model_name = model_name_array.last.underscore

return if underscore_model_name === 'attachment' ||
underscore_model_name === 'message' ||
underscore_model_name === 'config' ||
underscore_model_name === 'code' ||
underscore_model_name === 'public_storage' ||
underscore_model_name === 'cross_shared_collection'

if namespace == "setup" || namespace == "cenit"
describe "GET /api/v3/#{namespace}/#{underscore_model_name}" do
context "fail retrieve all existing #{underscore_model_name}" do
it "with missing header request" do
get "/api/v3/#{namespace}/#{underscore_model_name}"
expect(response).to have_http_status(:forbidden)
expect(json_response[:error]).to eq("insufficient_scope")
expect(json_response[:error_description]).to eq("The requested action is out of the access token scope")
end

it "with unauthorized token request" do
get "/api/v3/#{namespace}/#{underscore_model_name}", headers: {
Authorization: "Bearer unauthorized_token }"
}
expect(response).to have_http_status(:unauthorized)
expect(json_response[:error]).to eq("invalid_token")
expect(json_response[:error_description]).to eq("Malformed authorization header")
end
end

context "successful retrieve all existing #{underscore_model_name}" do
it "with authorized token request" do
get "/api/v3/#{namespace}/#{underscore_model_name}", headers: header_token_authorization
expect(response).to have_http_status(:ok)
expect(json_response[:count]).to be
expect(json_response[:current_page]).to be
expect(json_response[:data_type]).to be
expect(json_response[:items]).to be
expect(json_response[:total_pages]).to be
end
end
end
end
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

review in your editor and check if have an option to add empty line to a file

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'rails_helper'
RSpec.describe Api::V3::ApiController, type: :request do

describe "GET /api/v3/setup/execution.json" do

context "Fail retrieve all existing Executions" do
it "with missing header request" do
get '/api/v3/setup/execution.json'
expect(response).to have_http_status(:forbidden)
expect(json_response[:error]).to eq("insufficient_scope")
expect(json_response[:error_description]).to eq("The requested action is out of the access token scope")
end

it "with unauthorized token request" do
get '/api/v3/setup/execution.json', headers: {
Authorization: "Bearer unauthorized_token }"
}
expect(response).to have_http_status(:unauthorized)
expect(json_response[:error]).to eq("invalid_token")
expect(json_response[:error_description]).to eq("Malformed authorization header")
end
end

context "successful retrieve all existing Executions" do

it "with authorized token request" do
get '/api/v3/setup/execution.json', headers: header_token_authorization

expect(response).to have_http_status(:ok)
expect(json_response[:data_type]).to be
expect(json_response[:items]).to be
expect(json_response[:total_pages]).to be
end

end

end
end
41 changes: 41 additions & 0 deletions spec/requests/app/controllers/api/v3/monitors/storages_api_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'rails_helper'
RSpec.describe Api::V3::ApiController, type: :request do

describe "GET /api/v3/setup/storage.json" do

context "Fail retrieve all existing Storages" do
it "with missing header request" do
get '/api/v3/setup/storage.json'
expect(response).to have_http_status(:forbidden)
expect(json_response[:error]).to eq("insufficient_scope")
expect(json_response[:error_description]).to eq("The requested action is out of the access token scope")
end

it "with unauthorized token request" do
get '/api/v3/setup/storage.json', headers: {
Authorization: "Bearer unauthorized_token }"
}
expect(response).to have_http_status(:unauthorized)
expect(json_response[:error]).to eq("invalid_token")
expect(json_response[:error_description]).to eq("Malformed authorization header")
end
end

context "successful retrieve all existing Storages" do

it "with authorized token request" do
get '/api/v3/setup/storage.json', headers: header_token_authorization

expect(response).to have_http_status(:ok)
expect(json_response[:count]).to be
expect(json_response[:current_page]).to eq 1
expect(json_response[:current_page]).to be
expect(json_response[:data_type]).to be
expect(json_response[:items]).to be
expect(json_response[:total_pages]).to be
end

end

end
end
Loading