package ozon import ( "context" "net/http" "testing" core "git.denco.store/fakz9/ozon-api-client" ) func TestLeaveComment(t *testing.T) { t.Parallel() tests := []struct { statusCode int headers map[string]string params *LeaveCommentParams response string }{ // Test Ok { http.StatusOK, map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"}, &LeaveCommentParams{ MarkReviewAsProcesses: true, ParentCommentId: "string", ReviewId: "string1", Text: "some string", }, `{ "comment_id": "string" }`, }, // Test No Client-Id or Api-Key { http.StatusUnauthorized, map[string]string{}, &LeaveCommentParams{}, `{ "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.Reviews().LeaveComment(ctx, test.params) if err != nil { t.Error(err) continue } compareJsonResponse(t, test.response, &LeaveCommentResponse{}) if resp.StatusCode != test.statusCode { t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode) } } } func TestDeleteComment(t *testing.T) { t.Parallel() tests := []struct { statusCode int headers map[string]string params *DeleteCommentParams response string }{ // Test Ok { http.StatusOK, map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"}, &DeleteCommentParams{ CommentId: "string", }, `{}`, }, // Test No Client-Id or Api-Key { http.StatusUnauthorized, map[string]string{}, &DeleteCommentParams{}, `{ "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.Reviews().DeleteComment(ctx, test.params) if err != nil { t.Error(err) continue } compareJsonResponse(t, test.response, &DeleteCommentResponse{}) if resp.StatusCode != test.statusCode { t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode) } } } func TestListComments(t *testing.T) { t.Parallel() tests := []struct { statusCode int headers map[string]string params *ListCommentsParams response string }{ // Test Ok { http.StatusOK, map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"}, &ListCommentsParams{ Limit: 0, Offset: 0, ReviewId: "string", SortDir: Ascending, }, `{ "comments": [ { "id": "string", "is_official": true, "is_owner": true, "parent_comment_id": "string", "published_at": "2019-08-24T14:15:22Z", "text": "string" } ], "offset": 0 }`, }, // Test No Client-Id or Api-Key { http.StatusUnauthorized, map[string]string{}, &ListCommentsParams{}, `{ "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.Reviews().ListComments(ctx, test.params) if err != nil { t.Error(err) continue } compareJsonResponse(t, test.response, &ListCommentsResponse{}) if resp.StatusCode != test.statusCode { t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode) } } } func TestChangeStatus(t *testing.T) { t.Parallel() tests := []struct { statusCode int headers map[string]string params *ChangeStatusParams response string }{ // Test Ok { http.StatusOK, map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"}, &ChangeStatusParams{ ReviewIds: []string{"string"}, Status: "PROCESSED", }, `{}`, }, // Test No Client-Id or Api-Key { http.StatusUnauthorized, map[string]string{}, &ChangeStatusParams{}, `{ "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.Reviews().ChangeStatus(ctx, test.params) if err != nil { t.Error(err) continue } compareJsonResponse(t, test.response, &ChangeStatusResponse{}) if resp.StatusCode != test.statusCode { t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode) } } } func TestCountReviews(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"}, `{ "processed": 2, "total": 3, "unprocessed": 1 }`, }, // 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.Reviews().Count(ctx) if err != nil { t.Error(err) continue } compareJsonResponse(t, test.response, &CountReviewsResponse{}) if resp.StatusCode != test.statusCode { t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode) } } } func TestGetReview(t *testing.T) { t.Parallel() tests := []struct { statusCode int headers map[string]string params *GetReviewParams response string }{ // Test Ok { http.StatusOK, map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"}, &GetReviewParams{ ReviewId: "string", }, `{ "comments_amount": 0, "dislikes_amount": 0, "id": "string", "is_rating_participant": true, "likes_amount": 0, "order_status": "string", "photos": [ { "height": 0, "url": "string", "width": 0 } ], "photos_amount": 0, "published_at": "2019-08-24T14:15:22Z", "rating": 0, "sku": 0, "status": "string", "text": "string", "videos": [ { "height": 0, "preview_url": "string", "short_video_preview_url": "string", "url": "string", "width": 0 } ], "videos_amount": 0 }`, }, // Test No Client-Id or Api-Key { http.StatusUnauthorized, map[string]string{}, &GetReviewParams{}, `{ "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.Reviews().Get(ctx, test.params) if err != nil { t.Error(err) continue } compareJsonResponse(t, test.response, &GetReviewResponse{}) if resp.StatusCode != test.statusCode { t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode) } } } func TestListReviews(t *testing.T) { t.Parallel() tests := []struct { statusCode int headers map[string]string params *ListReviewsParams response string }{ // Test Ok { http.StatusOK, map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"}, &ListReviewsParams{ LastId: "string", Limit: 0, SortDir: Ascending, Status: "ALL", }, `{ "has_next": true, "last_id": "string", "reviews": [ { "comments_amount": 0, "id": "string", "is_rating_participant": true, "order_status": "string", "photos_amount": 0, "published_at": "2019-08-24T14:15:22Z", "rating": 0, "sku": 0, "status": "string", "text": "string", "videos_amount": 0 } ] }`, }, // Test No Client-Id or Api-Key { http.StatusUnauthorized, map[string]string{}, &ListReviewsParams{}, `{ "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.Reviews().List(ctx, test.params) if err != nil { t.Error(err) continue } compareJsonResponse(t, test.response, &ListReviewsResponse{}) if resp.StatusCode != test.statusCode { t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode) } } }