add api-keys endpoint.

This commit is contained in:
ZippyDev 2020-07-28 20:25:49 -06:00
parent 22da8eff6b
commit 0881970064
10 changed files with 279 additions and 5 deletions

View File

@ -0,0 +1,44 @@
# frozen_string_literal: true
module BtcPay
module Client
module Api
class ApiKeys < Base
PATH = '/api-keys'
# @see https://docs.btcpayserver.org/API/Greenfield/v1/#tag/API-Keys/paths/~1api~1v1~1api-keys~1current/get
def current(**opts)
client.get(path('current'), options: opts)
end
alias get current
alias info current
# @see https://docs.btcpayserver.org/API/Greenfield/v1/#tag/API-Keys/paths/~1api~1v1~1api-keys/post
def create(payload, **opts)
client.post(path, payload: payload, options: opts)
end
# @see https://docs.btcpayserver.org/API/Greenfield/v1/#tag/API-Keys/paths/~1api~1v1~1api-keys~1{apikey}/delete
def revoke(key, **opts)
client.delete(path(key), options: opts)
end
alias delete revoke
# https://docs.btcpayserver.org/API/Greenfield/v1/#tag/API-Keys/paths/~1api~1v1~1api-keys~1current/delete
def revoke!(**opts)
client.delete(path('current'), options: opts)
end
alias delete! revoke!
protected
def base_path
PATH
end
end
end
end
end

View File

@ -28,4 +28,5 @@ module BtcPay
end
end
require_relative './api_keys'
require_relative './users'

View File

@ -0,0 +1,45 @@
---
http_interactions:
- request:
method: delete
uri: http://localhost:49392/api/v1/api-keys/current
body:
encoding: US-ASCII
string: ''
headers:
Accept:
- application/json
User-Agent:
- btcpay_ruby/0.1.0
Content-Type:
- application/json
Accept-Encoding:
- deflate, gzip
Authorization:
- token 371010c12d8ef584e87c6968912cafd82953170e
Host:
- localhost:49392
response:
status:
code: 200
message: OK
headers:
Date:
- Wed, 29 Jul 2020 01:53:49 GMT
Server:
- Kestrel
Content-Length:
- '0'
Referrer-Policy:
- same-origin
X-Xss-Protection:
- 1; mode=block
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- DENY
body:
encoding: UTF-8
string: ''
recorded_at: Wed, 29 Jul 2020 01:53:50 GMT
recorded_with: VCR 6.0.0

View File

@ -0,0 +1,45 @@
---
http_interactions:
- request:
method: delete
uri: http://localhost:49392/api/v1/api-keys/75914ce4702f1f72f4bdfbddccf939b50947262d
body:
encoding: US-ASCII
string: ''
headers:
Accept:
- application/json
User-Agent:
- btcpay_ruby/0.1.0
Content-Type:
- application/json
Accept-Encoding:
- deflate, gzip
Authorization:
- token 9133b8ef3ae9a4b7f2d9a6efef1d5cf738067c68
Host:
- localhost:49392
response:
status:
code: 200
message: OK
headers:
Date:
- Wed, 29 Jul 2020 02:22:47 GMT
Server:
- Kestrel
Content-Length:
- '0'
Referrer-Policy:
- same-origin
X-Xss-Protection:
- 1; mode=block
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- DENY
body:
encoding: UTF-8
string: ''
recorded_at: Wed, 29 Jul 2020 02:22:48 GMT
recorded_with: VCR 6.0.0

View File

@ -0,0 +1,47 @@
---
http_interactions:
- request:
method: get
uri: http://localhost:49392/api/v1/api-keys/current
body:
encoding: US-ASCII
string: ''
headers:
Accept:
- application/json
User-Agent:
- btcpay_ruby/0.1.0
Content-Type:
- application/json
Accept-Encoding:
- deflate, gzip
Authorization:
- token 9133b8ef3ae9a4b7f2d9a6efef1d5cf738067c68
Host:
- localhost:49392
response:
status:
code: 200
message: OK
headers:
Date:
- Tue, 28 Jul 2020 00:41:43 GMT
Content-Type:
- application/json; charset=utf-8
Server:
- Kestrel
Content-Length:
- '592'
Referrer-Policy:
- same-origin
X-Xss-Protection:
- 1; mode=block
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- DENY
body:
encoding: UTF-8
string: '{"apiKey":"9133b8ef3ae9a4b7f2d9a6efef1d5cf738067c68","label":"local-all","permissions":["btcpay.store.cancreateinvoice","btcpay.server.canmodifyserversettings","btcpay.store.canmodifystoresettings","btcpay.store.canviewstoresettings","btcpay.store.canviewpaymentrequests","btcpay.store.canmodifypaymentrequests","btcpay.user.canmodifyprofile","btcpay.user.canviewprofile","btcpay.server.cancreateuser","unrestricted","btcpay.server.canuseinternallightningnode","btcpay.server.cancreatelightninginvoiceinternalnode","btcpay.store.canuselightningnode","btcpay.store.cancreatelightninginvoice"]}'
recorded_at: Tue, 28 Jul 2020 00:41:44 GMT
recorded_with: VCR 6.0.0

View File

@ -0,0 +1,49 @@
---
http_interactions:
- request:
method: post
uri: http://localhost:49392/api/v1/api-keys
body:
encoding: UTF-8
string: '{"label":"btcpaygem-spec-062490834fadd94abd09bcef2da08903","permissions":["unrestricted"]}'
headers:
Accept:
- application/json
User-Agent:
- btcpay_ruby/0.1.0
Content-Type:
- application/json
Accept-Encoding:
- deflate, gzip
Authorization:
- token 9133b8ef3ae9a4b7f2d9a6efef1d5cf738067c68
Content-Length:
- '90'
Host:
- localhost:49392
response:
status:
code: 200
message: OK
headers:
Date:
- Wed, 29 Jul 2020 02:23:09 GMT
Content-Type:
- application/json; charset=utf-8
Server:
- Kestrel
Content-Length:
- '142'
Referrer-Policy:
- same-origin
X-Xss-Protection:
- 1; mode=block
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- DENY
body:
encoding: UTF-8
string: '{"apiKey":"58ba8185aa87647d4b18b4d1f2503c6420b97552","label":"btcpaygem-spec-062490834fadd94abd09bcef2da08903","permissions":["unrestricted"]}'
recorded_at: Wed, 29 Jul 2020 02:23:10 GMT
recorded_with: VCR 6.0.0

View File

@ -0,0 +1,35 @@
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe BtcPay::Client::Api::ApiKeys, :vcr do
let(:client) { build(:client) }
subject { described_class.new(client: client) }
describe 'GET #current' do
let(:response) { subject.current }
it { expect(response).to be_success }
end
describe 'POST #create' do
let(:payload) { build(:api_keys_payload) }
let(:response) { subject.create(payload) }
it { expect(response).to be_success }
end
describe 'DELETE #revoke(:key)' do
let(:revoke_api_key) { '75914ce4702f1f72f4bdfbddccf939b50947262d' }
let(:response) { subject.revoke(revoke_api_key) }
it { expect(response).to be_success }
end
describe 'DELETE #revoke!' do
let(:client) { build(:client, config: build(:config, auth_token: '76e5404d713cfa3106607dc937315dd5f929bd29')) }
let(:response) { subject.revoke! }
it { expect(response).to be_success }
end
end

View File

@ -0,0 +1,10 @@
# frozen_string_literal: true
FactoryBot.define do
factory :api_keys_payload, class: Hash do
label { "btcpaygem-spec-#{SecureRandom.hex}" }
permissions { %w[unrestricted] }
initialize_with { attributes }
end
end

View File

@ -6,10 +6,6 @@ FactoryBot.define do
password { 'password' }
isAdministrator { false }
initialize_with do
user = {}
attributes.each { |k, v| user[k] = v }
user
end
initialize_with { attributes }
end
end

View File

@ -9,12 +9,14 @@ FactoryBot.define do
initialize_with { new(**attributes) }
trait :auth_token do
# this is a local instance of regtest so YOLO... replace as needed
auth_token { '9133b8ef3ae9a4b7f2d9a6efef1d5cf738067c68' }
basic_auth_token { nil }
end
trait :basic_auth_token do
auth_token { nil }
# this is a local instance of regtest so YOLO... replace as needed
basic_auth_token { 'G7Uvk0VV7BWQQI7LrihMnDGhEnR7kopW0PRoli77Lyo' }
end
end