761 lines
17 KiB
Go
761 lines
17 KiB
Go
package ozon
|
||
|
||
import (
|
||
"context"
|
||
"net/http"
|
||
"testing"
|
||
"time"
|
||
|
||
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))
|
||
|
||
ctx, _ := context.WithTimeout(context.Background(), testTimeout)
|
||
resp, err := c.Certificates().ListOfAccordanceTypes(ctx)
|
||
if err != nil {
|
||
t.Error(err)
|
||
continue
|
||
}
|
||
|
||
compareJsonResponse(t, test.response, &ListOfAccordanceTypesResponse{})
|
||
|
||
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))
|
||
|
||
ctx, _ := context.WithTimeout(context.Background(), testTimeout)
|
||
resp, err := c.Certificates().DirectoryOfDocumentTypes(ctx)
|
||
if err != nil {
|
||
t.Error(err)
|
||
continue
|
||
}
|
||
|
||
compareJsonResponse(t, test.response, &DirectoryOfDocumentTypesResponse{})
|
||
|
||
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))
|
||
|
||
ctx, _ := context.WithTimeout(context.Background(), testTimeout)
|
||
resp, err := c.Certificates().ListOfCertifiedCategories(ctx, test.params)
|
||
if err != nil {
|
||
t.Error(err)
|
||
continue
|
||
}
|
||
|
||
compareJsonResponse(t, test.response, &ListOfCertifiedCategoriesResponse{})
|
||
|
||
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))
|
||
|
||
ctx, _ := context.WithTimeout(context.Background(), testTimeout)
|
||
resp, err := c.Certificates().LinkToProduct(ctx, test.params)
|
||
if err != nil {
|
||
t.Error(err)
|
||
continue
|
||
}
|
||
|
||
compareJsonResponse(t, test.response, &LinkCertificateToProductResponse{})
|
||
|
||
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))
|
||
|
||
ctx, _ := context.WithTimeout(context.Background(), testTimeout)
|
||
resp, err := c.Certificates().Delete(ctx, test.params)
|
||
if err != nil {
|
||
t.Error(err)
|
||
continue
|
||
}
|
||
|
||
compareJsonResponse(t, test.response, &DeleteCertificateResponse{})
|
||
|
||
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))
|
||
|
||
ctx, _ := context.WithTimeout(context.Background(), testTimeout)
|
||
resp, err := c.Certificates().GetInfo(ctx, test.params)
|
||
if err != nil {
|
||
t.Error(err)
|
||
continue
|
||
}
|
||
|
||
compareJsonResponse(t, test.response, &GetCertificateInfoResponse{})
|
||
|
||
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))
|
||
|
||
ctx, _ := context.WithTimeout(context.Background(), testTimeout)
|
||
resp, err := c.Certificates().List(ctx, test.params)
|
||
if err != nil {
|
||
t.Error(err)
|
||
continue
|
||
}
|
||
|
||
compareJsonResponse(t, test.response, &ListCertificatesResponse{})
|
||
|
||
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))
|
||
|
||
ctx, _ := context.WithTimeout(context.Background(), testTimeout)
|
||
resp, err := c.Certificates().ProductStatuses(ctx)
|
||
if err != nil {
|
||
t.Error(err)
|
||
continue
|
||
}
|
||
|
||
compareJsonResponse(t, test.response, &ProductStatusesResponse{})
|
||
|
||
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))
|
||
|
||
ctx, _ := context.WithTimeout(context.Background(), testTimeout)
|
||
resp, err := c.Certificates().ListProductsForCertificate(ctx, test.params)
|
||
if err != nil {
|
||
t.Error(err)
|
||
continue
|
||
}
|
||
|
||
compareJsonResponse(t, test.response, &ListProductsForCertificateResponse{})
|
||
|
||
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))
|
||
|
||
ctx, _ := context.WithTimeout(context.Background(), testTimeout)
|
||
resp, err := c.Certificates().UnlinkFromProduct(ctx, test.params)
|
||
if err != nil {
|
||
t.Error(err)
|
||
continue
|
||
}
|
||
|
||
compareJsonResponse(t, test.response, &UnlinkFromProductResponse{})
|
||
|
||
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))
|
||
|
||
ctx, _ := context.WithTimeout(context.Background(), testTimeout)
|
||
resp, err := c.Certificates().PossibleRejectReasons(ctx)
|
||
if err != nil {
|
||
t.Error(err)
|
||
continue
|
||
}
|
||
|
||
compareJsonResponse(t, test.response, &PossibleRejectReasonsResponse{})
|
||
|
||
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))
|
||
|
||
ctx, _ := context.WithTimeout(context.Background(), testTimeout)
|
||
resp, err := c.Certificates().PossibleStatuses(ctx)
|
||
if err != nil {
|
||
t.Error(err)
|
||
continue
|
||
}
|
||
|
||
compareJsonResponse(t, test.response, &PossibleStatusesResponse{})
|
||
|
||
if resp.StatusCode != test.statusCode {
|
||
t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode)
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestAddCertificatesForProducts(t *testing.T) {
|
||
t.Parallel()
|
||
|
||
tests := []struct {
|
||
statusCode int
|
||
headers map[string]string
|
||
params *AddCertificatesForProductsParams
|
||
response string
|
||
}{
|
||
// Test Ok
|
||
{
|
||
http.StatusOK,
|
||
map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"},
|
||
&AddCertificatesForProductsParams{
|
||
Files: []byte{10, 15, 2, 0},
|
||
Name: "Certificate name",
|
||
Number: "10a-d5s9-4asdf2",
|
||
TypeCode: "declaration",
|
||
AccordanceTypeCode: "gost",
|
||
IssueDate: time.Now(),
|
||
ExpireDate: time.Now(),
|
||
},
|
||
`{
|
||
"id": 50058
|
||
}`,
|
||
},
|
||
// Test No Client-Id or Api-Key
|
||
{
|
||
http.StatusUnauthorized,
|
||
map[string]string{},
|
||
&AddCertificatesForProductsParams{},
|
||
`{
|
||
"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))
|
||
|
||
ctx, _ := context.WithTimeout(context.Background(), testTimeout)
|
||
resp, err := c.Certificates().AddForProducts(ctx, test.params)
|
||
if err != nil {
|
||
t.Error(err)
|
||
continue
|
||
}
|
||
|
||
compareJsonResponse(t, test.response, &AddCertificatesForProductsResponse{})
|
||
|
||
if resp.StatusCode != test.statusCode {
|
||
t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode)
|
||
}
|
||
}
|
||
}
|