add methods for quality certificates

This commit is contained in:
diPhantxm
2023-03-23 23:41:02 +03:00
parent 89816cb239
commit 3af6ad40c5
4 changed files with 1170 additions and 13 deletions

View File

@@ -54,20 +54,19 @@
- [x] List of certified brands
## Quality certificates
- [ ] List of accordance types (version 1)
- [ ] List of accordance types (version 2)
- [ ] Directory of document types
- [ ] List of certified categories
- [x] List of accordance types (version 2)
- [x] Directory of document types
- [x] List of certified categories
- [ ] Adding certificates for products
- [ ] Link the certificate to the product
- [ ] Delete certificate
- [ ] Certificate information
- [ ] Certificates list
- [ ] Product statuses list
- [ ] List of products associated with the certificate
- [ ] Unbind products from a certificate
- [ ] Possible certificate rejection reasons
- [ ] Possible certificate statuses
- [x] Link the certificate to the product
- [x] Delete certificate
- [x] Certificate information
- [x] Certificates list
- [x] Product statuses list
- [x] List of products associated with the certificate
- [x] Unbind products from a certificate
- [x] Possible certificate rejection reasons
- [x] Possible certificate statuses
## Warehouses
- [x] List of warehouses

497
ozon/certificates.go Normal file
View File

@@ -0,0 +1,497 @@
package ozon
import (
"net/http"
"time"
core "github.com/diphantxm/ozon-api-client"
)
type Certificates struct {
client *core.Client
}
type ListOfAccordanceTypesResponse struct {
core.CommonResponse
// Accordance types
Result struct {
// Main accordance types
Base []struct {
// Accordance type code
Code string `json:"code"`
// Accordance type description
Title string `json:"title"`
} `json:"base"`
// Main accordance types related to dangerous products
Hazard []struct {
// Accordance type code
Code string `json:"code"`
// Accordance type description
Title string `json:"title"`
} `json:"hazard"`
} `json:"result"`
}
// List of accordance types (version 2)
func (c Certificates) ListOfAccordanceTypes() (*ListOfAccordanceTypesResponse, error) {
url := "/v2/product/certificate/accordance-types/list"
resp := &ListOfAccordanceTypesResponse{}
response, err := c.client.Request(http.MethodGet, url, nil, resp)
if err != nil {
return nil, err
}
response.CopyCommonResponse(&resp.CommonResponse)
return resp, nil
}
type DirectoryOfDocumentTypesResponse struct {
core.CommonResponse
// List of certificate types and names
Result []struct {
// Certificate name
Name string `json:"name"`
// Certificate type
Value string `json:"value"`
} `json:"result"`
}
// Directory of document types
func (c Certificates) DirectoryOfDocumentTypes() (*DirectoryOfDocumentTypesResponse, error) {
url := "/v1/product/certificate/types"
resp := &DirectoryOfDocumentTypesResponse{}
response, err := c.client.Request(http.MethodGet, url, nil, resp)
if err != nil {
return nil, err
}
response.CopyCommonResponse(&resp.CommonResponse)
return resp, nil
}
type ListOfCertifiedCategoriesParams struct {
// Number of the page returned in the query
Page int32 `json:"page"`
// Number of elements on the page
PageSize int32 `json:"page_size"`
}
type ListOfCertifiedCategoriesResponse struct {
core.CommonResponse
// Method result
Result struct {
// Certified categories details
Certification []struct {
// Category name
CategoryName string `json:"category_name"`
// Indication of a mandatory category
IsRequired bool `json:"is_required"`
} `json:"certification"`
// Total number of categories
Total int64 `json:"total"`
} `json:"reult"`
}
// List of certified categories
func (c Certificates) ListOfCertifiedCategories(params *ListOfCertifiedCategoriesParams) (*ListOfCertifiedCategoriesResponse, error) {
url := "/v1/product/certificate/types"
resp := &ListOfCertifiedCategoriesResponse{}
response, err := c.client.Request(http.MethodPost, url, params, resp)
if err != nil {
return nil, err
}
response.CopyCommonResponse(&resp.CommonResponse)
return resp, nil
}
type LinkCertificateToProductParams struct {
// Certificate identifier that was assigned when it was uploaded
CertificateId int64 `json:"certificate_id"`
// An array of product identifiers that this certificate applies to
ProductId []int64 `json:"product_id"`
}
type LinkCertificateToProductResponse struct {
core.CommonResponse
// The result of processing the request. true if the request was executed without errors
Result bool `json:"result"`
}
// Link the certificate to the product
func (c Certificates) LinkToProduct(params *LinkCertificateToProductParams) (*LinkCertificateToProductResponse, error) {
url := "/v1/product/certificate/bind"
resp := &LinkCertificateToProductResponse{}
response, err := c.client.Request(http.MethodPost, url, params, resp)
if err != nil {
return nil, err
}
response.CopyCommonResponse(&resp.CommonResponse)
return resp, nil
}
type DeleteCertificateParams struct {
// Certificate identifier
CertificateId int32 `json:"certificate_id"`
}
type DeleteCertificateResponse struct {
core.CommonResponse
// Result of deleting the certificate
Result struct {
// Indication that a certificate has been deleted:
// - true — deleted
// - false — not deleted
IsDelete bool `json:"is_delete"`
// Description of errors during certificate deletion
ErrorMessage string `json:"error_message"`
} `json:"result"`
}
// Delete certificate
func (c Certificates) Delete(params *DeleteCertificateParams) (*DeleteCertificateResponse, error) {
url := "/v1/product/certificate/delete"
resp := &DeleteCertificateResponse{}
response, err := c.client.Request(http.MethodPost, url, params, resp)
if err != nil {
return nil, err
}
response.CopyCommonResponse(&resp.CommonResponse)
return resp, nil
}
type GetCertificateInfoParams struct {
// Certificate identifier
CertificateNumber string `json:"certificate_number"`
}
type GetCertificateInfoResponse struct {
core.CommonResponse
// Certificate information
Result struct {
// Identifier
CertificateId int32 `json:"certificate_id"`
// Number
CertificateNumber string `json:"certificate_number"`
// Name
CertificateName string `json:"certificate_name"`
// Type
TypeCode string `json:"type_code"`
// Status
StatusCode string `json:"status_code"`
// Accordance type
AccordanceTypeCode string `json:"accordance_type_code"`
// Certificate rejection reason
RejectionReasonCode string `json:"rejectio_reason_code"`
// Moderator's comment
VerificationComment string `json:"verification_comment"`
// Issue date
IssueDate time.Time `json:"issue_date"`
// Expire date
ExpireDate time.Time `json:"expire_date"`
// Number of products associated with a certificate
ProductsCount int32 `json:"products_count"`
} `json:"result"`
}
// Certificate information
func (c Certificates) GetInfo(params *GetCertificateInfoParams) (*GetCertificateInfoResponse, error) {
url := "/v1/product/certificate/info"
resp := &GetCertificateInfoResponse{}
response, err := c.client.Request(http.MethodPost, url, params, resp)
if err != nil {
return nil, err
}
response.CopyCommonResponse(&resp.CommonResponse)
return resp, nil
}
type ListCertificatesParams struct {
// Product identifier associated with the certificate.
// Pass the parameter if you need certificates that certain products are associated with
OfferId string `json:"offer_id"`
// Certificate status. Pass the parameter if you need certificates with a certain status
Status string `json:"status"`
// Certificate type. Pass the parameter if you need certificates with a certain type
Type string `json:"type"`
// Page from which the list should be displayed. The minimum value is 1
Page int32 `json:"page"`
// Number of objects on the page. The value is from 1 to 1000
PageSize int32 `json:"page_size"`
}
type ListCertificatesResponse struct {
core.CommonResponse
// Certificates
Result struct {
// Сertificate information
Certificates []struct {
// Identifier
CertificateId int32 `json:"certificate_id"`
// Number
CertificateNumber string `json:"certificate_number"`
// Name
CertificateName string `json:"certificate_name"`
// Type
TypeCode string `json:"type"`
// Status
StatusCode string `json:"status_code"`
// Accordance type
AccordanceTypecode string `json:"accordance_type_code"`
// Certificate rejection reason
RejectionReasonCode string `json:"rejectio_reason_code"`
// Moderator's comment
VerificationComment string `json:"verification_comment"`
// Issue date
IssueDate time.Time `json:"issue_data"`
// Expire date
ExpireDate time.Time `json:"expire_date"`
// Number of products associated with a certificate
ProductsCount int32 `json:"products_count"`
} `json:"certificates"`
// Number of pages
PageCount int32 `json:"page_count"`
} `json:"result"`
}
// Certificates list
func (c Certificates) List(params *ListCertificatesParams) (*ListCertificatesResponse, error) {
url := "/v1/product/certificate/list"
resp := &ListCertificatesResponse{}
response, err := c.client.Request(http.MethodPost, url, params, resp)
if err != nil {
return nil, err
}
response.CopyCommonResponse(&resp.CommonResponse)
return resp, nil
}
type ProductStatusesResponse struct {
core.CommonResponse
// Product statuses
Result []struct {
// Product status code when linking it to the certificate
Code string `json:"code"`
// Status description
Name string `json:"name"`
} `json:"result"`
}
func (c Certificates) ProductStatuses() (*ProductStatusesResponse, error) {
url := "/v1/product/certificate/list"
resp := &ProductStatusesResponse{}
response, err := c.client.Request(http.MethodPost, url, nil, resp)
if err != nil {
return nil, err
}
response.CopyCommonResponse(&resp.CommonResponse)
return resp, nil
}
type ListProductsForCertificateParams struct {
// Certificate identifier
CertificateId int32 `json:"certificate_id"`
// Status of the product verification when binding to a certificate
ProductStatusCode string `json:"product_status_code"`
// Page from which the list should be displayed. The minimum value is 1
Page int32 `json:"page"`
// Number of objects on the page. The value is from 1 to 1000
PageSize int32 `json:"page_size"`
}
type ListProductsForCertificateResponse struct {
core.CommonResponse
// Method result
Result struct {
// List of products
Items []struct {
// Product identifier
ProductId int64 `json:"product_id"`
// Status of the product processing when binding to a certificate
ProductStatusCode string `json:"product_status_code"`
} `json:"items"`
// Number of products found
Count int64 `json:"count"`
} `json:"result"`
}
// A method for getting a list of possible statuses of products when binding them to a certificate
func (c Certificates) ListProductsForCertificate(params *ListProductsForCertificateParams) (*ListProductsForCertificateResponse, error) {
url := "/v1/product/certificate/products/list"
resp := &ListProductsForCertificateResponse{}
response, err := c.client.Request(http.MethodPost, url, params, resp)
if err != nil {
return nil, err
}
response.CopyCommonResponse(&resp.CommonResponse)
return resp, nil
}
type UnlinkFromProductParams struct {
// Certificate identifier
CertificateId int32 `json:"certificate_id"`
// List of product identifiers that you want to unbind from a certificate
ProductId []int64 `json:"product_id"`
}
type UnlinkFromProductResponse struct {
core.CommonResponse
// Method result
Result []struct {
// Error message when unbinding a product
Error string `json:"error"`
// Product identifier
ProductId int64 `json:"product_id"`
// Indication that the product was unbound from a certificate:
// - true — it was unbound,
// - false — it is still bound
Updated bool `json:"updated"`
} `json:"result"`
}
// Unbind products from a certificate
func (c Certificates) UnlinkFromProduct(params *UnlinkFromProductParams) (*UnlinkFromProductResponse, error) {
url := "/v1/product/certificate/unbind"
resp := &UnlinkFromProductResponse{}
response, err := c.client.Request(http.MethodPost, url, params, resp)
if err != nil {
return nil, err
}
response.CopyCommonResponse(&resp.CommonResponse)
return resp, nil
}
type PossibleRejectReasonsResponse struct {
core.CommonResponse
// Certificate rejection reasons
Result []struct {
// Сode of a certificate rejection reason
Code string `json:"code"`
// Description of a certificate rejection reason
Name string `json:"name"`
} `json:"result"`
}
// Possible certificate rejection reasons
func (c Certificates) PossibleRejectReasons() (*PossibleRejectReasonsResponse, error) {
url := "/v1/product/certificate/rejection_reasons/list"
resp := &PossibleRejectReasonsResponse{}
response, err := c.client.Request(http.MethodPost, url, nil, resp)
if err != nil {
return nil, err
}
response.CopyCommonResponse(&resp.CommonResponse)
return resp, nil
}
type PossibleStatusesResponse struct {
core.CommonResponse
// Possible certificate statuses
Result []struct {
// Certificate status code
Code string `json:"code"`
// Status description
Name string `json:"name"`
} `json:"result"`
}
func (c Certificates) PossibleStatuses() (*PossibleStatusesResponse, error) {
url := "/v1/product/certificate/status/list"
resp := &PossibleStatusesResponse{}
response, err := c.client.Request(http.MethodPost, url, nil, resp)
if err != nil {
return nil, err
}
response.CopyCommonResponse(&resp.CommonResponse)
return resp, nil
}

654
ozon/certificates_test.go Normal file
View File

@@ -0,0 +1,654 @@
package ozon
import (
"net/http"
"testing"
core "github.com/diphantxm/ozon-api-client"
)
func TestListOfAccordanceTypes(t *testing.T) {
t.Parallel()
tests := []struct {
statusCode int
headers map[string]string
response string
}{
// Test Ok
{
http.StatusOK,
map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"},
`{
"result": {
"base": [
{
"code": "string",
"title": "string"
}
],
"hazard": [
{
"code": "string",
"title": "string"
}
]
}
}`,
},
// Test No Client-Id or Api-Key
{
http.StatusUnauthorized,
map[string]string{},
`{
"code": 16,
"message": "Client-Id and Api-Key headers are required"
}`,
},
}
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.Certificates().ListOfAccordanceTypes()
if err != nil {
t.Error(err)
}
if resp.StatusCode != test.statusCode {
t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode)
}
}
}
func TestDirectoryOfDocumentTypes(t *testing.T) {
t.Parallel()
tests := []struct {
statusCode int
headers map[string]string
response string
}{
// Test Ok
{
http.StatusOK,
map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"},
`{
"result": [
{
"name": "Сертификат соответствия",
"value": "certificate_of_conformity"
},
{
"name": "Декларация",
"value": "declaration"
},
{
"name": "Свидетельство о гос регистрации",
"value": "certificate_of_registration"
},
{
"name": "Регистрационное удостоверение",
"value": "registration_certificate"
},
{
"name": "Отказное письмо",
"value": "refused_letter"
}
]
}`,
},
// Test No Client-Id or Api-Key
{
http.StatusUnauthorized,
map[string]string{},
`{
"code": 16,
"message": "Client-Id and Api-Key headers are required"
}`,
},
}
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.Certificates().DirectoryOfDocumentTypes()
if err != nil {
t.Error(err)
}
if resp.StatusCode != test.statusCode {
t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode)
}
}
}
func TestListOfCertifiedCategories(t *testing.T) {
t.Parallel()
tests := []struct {
statusCode int
headers map[string]string
params *ListOfCertifiedCategoriesParams
response string
}{
// Test Ok
{
http.StatusOK,
map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"},
&ListOfCertifiedCategoriesParams{
Page: 1,
PageSize: 100,
},
`{
"result": {
"certification": [
{
"is_required": true,
"category_name": "Витаминно-минеральные комплексы для взрослых"
}
],
"total": 1
}
}`,
},
// Test No Client-Id or Api-Key
{
http.StatusUnauthorized,
map[string]string{},
&ListOfCertifiedCategoriesParams{},
`{
"code": 16,
"message": "Client-Id and Api-Key headers are required"
}`,
},
}
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.Certificates().ListOfCertifiedCategories(test.params)
if err != nil {
t.Error(err)
}
if resp.StatusCode != test.statusCode {
t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode)
}
}
}
func TestLinkCertificateToProduct(t *testing.T) {
t.Parallel()
tests := []struct {
statusCode int
headers map[string]string
params *LinkCertificateToProductParams
response string
}{
// Test Ok
{
http.StatusOK,
map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"},
&LinkCertificateToProductParams{
CertificateId: 50058,
ProductId: []int64{290},
},
`{
"result": true
}`,
},
// Test No Client-Id or Api-Key
{
http.StatusUnauthorized,
map[string]string{},
&LinkCertificateToProductParams{},
`{
"code": 16,
"message": "Client-Id and Api-Key headers are required"
}`,
},
}
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.Certificates().LinkToProduct(test.params)
if err != nil {
t.Error(err)
}
if resp.StatusCode != test.statusCode {
t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode)
}
}
}
func TestDeleteCertificate(t *testing.T) {
t.Parallel()
tests := []struct {
statusCode int
headers map[string]string
params *DeleteCertificateParams
response string
}{
// Test Ok
{
http.StatusOK,
map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"},
&DeleteCertificateParams{
CertificateId: 0,
},
`{
"result": {
"is_delete": true,
"error_message": "string"
}
}`,
},
// Test No Client-Id or Api-Key
{
http.StatusUnauthorized,
map[string]string{},
&DeleteCertificateParams{},
`{
"code": 16,
"message": "Client-Id and Api-Key headers are required"
}`,
},
}
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.Certificates().Delete(test.params)
if err != nil {
t.Error(err)
}
if resp.StatusCode != test.statusCode {
t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode)
}
}
}
func TestGetCertificateInfo(t *testing.T) {
t.Parallel()
tests := []struct {
statusCode int
headers map[string]string
params *GetCertificateInfoParams
response string
}{
// Test Ok
{
http.StatusOK,
map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"},
&GetCertificateInfoParams{
CertificateNumber: "certificate number",
},
`{
"result": {
"certificate_id": 0,
"certificate_number": "certificate number",
"certificate_name": "string",
"type_code": "string",
"status_code": "string",
"accordance_type_code": "string",
"rejection_reason_code": "string",
"verification_comment": "string",
"issue_date": "2019-08-24T14:15:22Z",
"expire_date": "2019-08-24T14:15:22Z",
"products_count": 0
}
}`,
},
// Test No Client-Id or Api-Key
{
http.StatusUnauthorized,
map[string]string{},
&GetCertificateInfoParams{},
`{
"code": 16,
"message": "Client-Id and Api-Key headers are required"
}`,
},
}
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.Certificates().GetInfo(test.params)
if err != nil {
t.Error(err)
}
if resp.StatusCode != test.statusCode {
t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode)
}
if resp.StatusCode == http.StatusOK {
if resp.Result.CertificateNumber != test.params.CertificateNumber {
t.Errorf("Certificate numbers in request and response are not equal")
}
}
}
}
func TestListCertificates(t *testing.T) {
t.Parallel()
tests := []struct {
statusCode int
headers map[string]string
params *ListCertificatesParams
response string
}{
// Test Ok
{
http.StatusOK,
map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"},
&ListCertificatesParams{
OfferId: "id",
Status: "some status",
Type: "some type",
Page: 1,
PageSize: 1,
},
`{
"result": {
"certificates": [
{
"certificate_id": 0,
"certificate_number": "string",
"certificate_name": "string",
"type_code": "string",
"status_code": "string",
"accordance_type_code": "string",
"rejection_reason_code": "string",
"verification_comment": "string",
"issue_date": "2019-08-24T14:15:22Z",
"expire_date": "2019-08-24T14:15:22Z",
"products_count": 0
}
],
"page_count": 0
}
}`,
},
// Test No Client-Id or Api-Key
{
http.StatusUnauthorized,
map[string]string{},
&ListCertificatesParams{},
`{
"code": 16,
"message": "Client-Id and Api-Key headers are required"
}`,
},
}
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.Certificates().List(test.params)
if err != nil {
t.Error(err)
}
if resp.StatusCode != test.statusCode {
t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode)
}
}
}
func TestProductStatuses(t *testing.T) {
t.Parallel()
tests := []struct {
statusCode int
headers map[string]string
response string
}{
// Test Ok
{
http.StatusOK,
map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"},
`{
"result": [
{
"code": "string",
"name": "string"
}
]
}`,
},
// Test No Client-Id or Api-Key
{
http.StatusUnauthorized,
map[string]string{},
`{
"code": 16,
"message": "Client-Id and Api-Key headers are required"
}`,
},
}
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.Certificates().ProductStatuses()
if err != nil {
t.Error(err)
}
if resp.StatusCode != test.statusCode {
t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode)
}
}
}
func TestListProductsForCertificate(t *testing.T) {
t.Parallel()
tests := []struct {
statusCode int
headers map[string]string
params *ListProductsForCertificateParams
response string
}{
// Test Ok
{
http.StatusOK,
map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"},
&ListProductsForCertificateParams{
CertificateId: 0,
ProductStatusCode: "status code",
Page: 0,
PageSize: 0,
},
`{
"result": {
"items": [
{
"product_id": 0,
"product_status_code": "string"
}
],
"count": 0
}
}`,
},
// Test No Client-Id or Api-Key
{
http.StatusUnauthorized,
map[string]string{},
&ListProductsForCertificateParams{},
`{
"code": 16,
"message": "Client-Id and Api-Key headers are required"
}`,
},
}
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.Certificates().ListProductsForCertificate(test.params)
if err != nil {
t.Error(err)
}
if resp.StatusCode != test.statusCode {
t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode)
}
}
}
func TestUnlinkFromProduct(t *testing.T) {
t.Parallel()
tests := []struct {
statusCode int
headers map[string]string
params *UnlinkFromProductParams
response string
}{
// Test Ok
{
http.StatusOK,
map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"},
&UnlinkFromProductParams{
CertificateId: 0,
ProductId: []int64{0},
},
`{
"result": [
{
"error": "string",
"product_id": 0,
"updated": true
}
]
}`,
},
// Test No Client-Id or Api-Key
{
http.StatusUnauthorized,
map[string]string{},
&UnlinkFromProductParams{},
`{
"code": 16,
"message": "Client-Id and Api-Key headers are required"
}`,
},
}
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.Certificates().UnlinkFromProduct(test.params)
if err != nil {
t.Error(err)
}
if resp.StatusCode != test.statusCode {
t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode)
}
}
}
func TestPossibleRejectReasons(t *testing.T) {
t.Parallel()
tests := []struct {
statusCode int
headers map[string]string
response string
}{
// Test Ok
{
http.StatusOK,
map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"},
`{
"result": [
{
"code": "string",
"name": "string"
}
]
}`,
},
// Test No Client-Id or Api-Key
{
http.StatusUnauthorized,
map[string]string{},
`{
"code": 16,
"message": "Client-Id and Api-Key headers are required"
}`,
},
}
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.Certificates().PossibleRejectReasons()
if err != nil {
t.Error(err)
}
if resp.StatusCode != test.statusCode {
t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode)
}
}
}
func TestPossibleStatuses(t *testing.T) {
t.Parallel()
tests := []struct {
statusCode int
headers map[string]string
response string
}{
// Test Ok
{
http.StatusOK,
map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"},
`{
"result": [
{
"code": "string",
"name": "string"
}
]
}`,
},
// Test No Client-Id or Api-Key
{
http.StatusUnauthorized,
map[string]string{},
`{
"code": 16,
"message": "Client-Id and Api-Key headers are required"
}`,
},
}
for _, test := range tests {
c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers))
resp, err := c.Certificates().PossibleStatuses()
if err != nil {
t.Error(err)
}
if resp.StatusCode != test.statusCode {
t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode)
}
}
}

View File

@@ -29,6 +29,7 @@ type Client struct {
invoices *Invoices
brands *Brands
chats *Chats
certificates *Certificates
}
func (c Client) Analytics() *Analytics {
@@ -95,6 +96,10 @@ func (c Client) Chats() *Chats {
return c.chats
}
func (c Client) Certificates() *Certificates {
return c.certificates
}
func NewClient(clientId, apiKey string) *Client {
coreClient := core.NewClient(DefaultAPIBaseUrl, map[string]string{
"Client-Id": clientId,
@@ -119,6 +124,7 @@ func NewClient(clientId, apiKey string) *Client {
invoices: &Invoices{client: coreClient},
brands: &Brands{client: coreClient},
chats: &Chats{client: coreClient},
certificates: &Certificates{client: coreClient},
}
}
@@ -143,5 +149,6 @@ func NewMockClient(handler http.HandlerFunc) *Client {
invoices: &Invoices{client: coreClient},
brands: &Brands{client: coreClient},
chats: &Chats{client: coreClient},
certificates: &Certificates{client: coreClient},
}
}