Merge pull request #2105 from internetee/implement-api-endpoint-for-login-info

added api endpoint for registrar login
This commit is contained in:
Timo Võhmar 2021-11-10 13:36:43 +02:00 committed by GitHub
commit c5719a35f1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 511 additions and 7 deletions

View file

@ -0,0 +1,33 @@
require 'test_helper'
class AuthTest < ApplicationIntegrationTest
def setup
super
@user = users(:api_bestnames)
@header = { 'Authorization' => "Basic #{generate_base64}" }
end
def test_should_return_successful
get 'https://registry.test/api/v1/accreditation_center/auth', headers: @header
json = JSON.parse(response.body, symbolize_names: true)
assert_equal json[:code], 1000
assert_equal json[:message], 'Command completed successfully'
end
def test_should_return_failed
get 'https://registry.test/api/v1/accreditation_center/auth', headers: { 'Authorization' => "Basic LAHSDHDSAFSF#@" }
json = JSON.parse(response.body, symbolize_names: true)
assert_equal json[:code], 2202
assert_equal json[:message], 'Invalid authorization information'
end
private
def generate_base64
Base64.encode64("#{@user.username}:#{@user.plain_text_password}")
end
end

View file

@ -0,0 +1,23 @@
require 'test_helper'
class ContactsTest < ApplicationIntegrationTest
def setup
super
@contact = contacts(:john)
end
def test_return_code_error_if_valid_domain_name
get '/api/v1/accreditation_center/contacts/?id=Alyosha'
json = JSON.parse(response.body, symbolize_names: true)
assert_equal json[:errors], 'Contact not found'
end
def test_return_code_error_if_sdfsdf
get "/api/v1/accreditation_center/contacts/?id=#{@contact.code}"
json = JSON.parse(response.body, symbolize_names: true)
assert_equal json[:contact][:name], 'John'
end
end

View file

@ -0,0 +1,21 @@
require 'test_helper'
class DomainsTest < ApplicationIntegrationTest
def setup
@domain = domains(:shop)
end
def test_get_domain_info
get "/api/v1/accreditation_center/domains/?name=shop.test"
json = JSON.parse(response.body, symbolize_names: true)
assert_equal json[:domain][:name], "shop.test"
end
def test_return_code_error_if_valid_domain_name
get "/api/v1/accreditation_center/domains/?name=some.ee"
json = JSON.parse(response.body, symbolize_names: true)
assert_equal json[:errors], "Domain not found"
end
end

View file

@ -0,0 +1,33 @@
require 'test_helper'
class DomainsTest < ApplicationIntegrationTest
def setup
super
@user = users(:api_bestnames)
@header = { 'Authorization' => "Basic #{generate_base64}" }
end
def test_should_return_cancelled_invoices
date_now = Time.now
get "/api/v1/accreditation_center/invoice_status", headers: @header
json = JSON.parse(response.body, symbolize_names: true)
assert_equal json[:invoices].count, 0
invoice = @user.registrar.invoices.last
invoice.update(cancelled_at: date_now)
get "/api/v1/accreditation_center/invoice_status", headers: @header
json = JSON.parse(response.body, symbolize_names: true)
assert_equal json[:invoices].count, 1
end
private
def generate_base64
Base64.encode64("#{@user.username}:#{@user.plain_text_password}")
end
end

View file

@ -0,0 +1,35 @@
require 'test_helper'
class ReppV1AccreditationInfoTest < ActionDispatch::IntegrationTest
def setup
@user = users(:api_bestnames)
token = Base64.encode64("#{@user.username}:#{@user.plain_text_password}")
token = "Basic #{token}"
@auth_headers = { 'Authorization' => token }
end
def test_valid_login
get '/repp/v1/registrar/accreditation/get_info', headers: @auth_headers
json = JSON.parse(response.body, symbolize_names: true)
assert_response :ok
assert_equal json[:data][:username], @user.username
assert json[:data][:roles].include? 'super'
assert_equal json[:data][:registrar_name], 'Best Names'
assert_equal json[:data][:registrar_reg_no], '1234'
end
def test_invalid_login
token = Base64.encode64("#{@user.username}:0066600")
token = "Basic #{token}"
auth_headers = { 'Authorization' => token }
get '/repp/v1/registrar/accreditation/get_info', headers: auth_headers
json = JSON.parse(response.body, symbolize_names: true)
assert_response :unauthorized
assert_equal json[:message], 'Invalid authorization information'
end
end

View file

@ -0,0 +1,49 @@
require 'test_helper'
class ReppV1AccreditationResultsTest < ActionDispatch::IntegrationTest
TEMPORARY_SECRET_KEY = ENV['accreditation_secret'].freeze
def setup
@user = users(:api_bestnames)
token = "Basic #{TEMPORARY_SECRET_KEY}"
@auth_headers = { 'Authorization' => token }
end
def test_should_return_valid_response
post '/repp/v1/registrar/accreditation/push_results',
headers: @auth_headers,
params: {accreditation_result: {username: @user.username, result: true} }
json = JSON.parse(response.body, symbolize_names: true)
assert_response :ok
assert_equal json[:data][:user][:username], @user.username
assert_equal json[:data][:result], "true"
assert_equal json[:data][:message], "Accreditation info successfully added"
end
def test_should_return_valid_response_invalid_authorization
post '/repp/v1/registrar/accreditation/push_results',
headers: { 'Authorization' => 'Basic temporary-secret-ke'},
params: {accreditation_result: {username: @user.username, result: true} }
json = JSON.parse(response.body, symbolize_names: true)
assert_response :unauthorized
assert_equal json[:code], 2202
assert_equal json[:message], 'Invalid authorization information'
end
def test_should_return_valid_response_record_exception
post '/repp/v1/registrar/accreditation/push_results',
headers: @auth_headers,
params: {accreditation_result: { username: "chungachanga", result: true} }
json = JSON.parse(response.body, symbolize_names: true)
assert_response :ok
assert_equal json[:code], 2303
assert_equal json[:message], "Object 'chungachanga' does not exist"
end
end