|
| 1 | + |
| 2 | +using System.Net; |
| 3 | +using System.Threading.Tasks; |
| 4 | + |
| 5 | +using Moq; |
| 6 | +using Newtonsoft.Json.Linq; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +using Crowdin.Api.Core; |
| 10 | +using Crowdin.Api.Reports; |
| 11 | +using Crowdin.Api.Tests.Core; |
| 12 | + |
| 13 | +namespace Crowdin.Api.Tests.Reports |
| 14 | +{ |
| 15 | + public class GroupReportsApiTests |
| 16 | + { |
| 17 | + private const int groupId = 1; |
| 18 | + private const int reportId = 2; |
| 19 | + private readonly JObject mockResponseObject = JObject.Parse(@" |
| 20 | + { |
| 21 | + ""identifier"": ""50fb3506-4127-4ba8-8296-f97dc7e3e0c3"", |
| 22 | + ""status"": ""finished"", |
| 23 | + ""progress"": 100, |
| 24 | + ""attributes"": { |
| 25 | + ""format"": ""xlsx"", |
| 26 | + ""reportName"": ""costs-estimation"", |
| 27 | + ""schema"": {} |
| 28 | + }, |
| 29 | + ""createdAt"": ""2019-09-23T11:26:54+00:00"", |
| 30 | + ""updatedAt"": ""2019-09-23T11:26:54+00:00"", |
| 31 | + ""startedAt"": ""2019-09-23T11:26:54+00:00"", |
| 32 | + ""finishedAt"": ""2019-09-23T11:26:54+00:00"", |
| 33 | + ""eta"": ""1 second"" |
| 34 | + } |
| 35 | + "); |
| 36 | + |
| 37 | + [Fact] |
| 38 | + public async Task GenerateGroupReport_GroupTranslationCost() |
| 39 | + { |
| 40 | + var request = new GroupTranslationCostGenerateGroupReportRequest |
| 41 | + { |
| 42 | + Schema = new GroupTranslationCostGenerateGroupReportRequest.RequestSchema |
| 43 | + { |
| 44 | + Unit = ReportUnit.Words, |
| 45 | + Currency = ReportCurrency.USD, |
| 46 | + Format = ReportFormat.Xlsx |
| 47 | + } |
| 48 | + }; |
| 49 | + |
| 50 | + Mock<ICrowdinApiClient> mockClient = TestUtils.CreateMockClientWithDefaultParser(); |
| 51 | + |
| 52 | + var url = $"/groups/{groupId}/reports"; |
| 53 | + |
| 54 | + mockClient |
| 55 | + .Setup(client => client.SendPostRequest(url, request, null)) |
| 56 | + .ReturnsAsync(new CrowdinApiResult |
| 57 | + { |
| 58 | + StatusCode = HttpStatusCode.OK, |
| 59 | + JsonObject = mockResponseObject |
| 60 | + }); |
| 61 | + |
| 62 | + var executor = new ReportsApiExecutor(mockClient.Object); |
| 63 | + var response = await executor.GenerateGroupReport(groupId, request); |
| 64 | + |
| 65 | + Assert.NotNull(response); |
| 66 | + Assert.IsType<GroupReportStatus>(response); |
| 67 | + } |
| 68 | + |
| 69 | + [Fact] |
| 70 | + public async Task GenerateGroupReport_GroupTopMembers() |
| 71 | + { |
| 72 | + var request = new GroupTopMembersGenerateGroupReportRequest |
| 73 | + { |
| 74 | + Schema = new GroupTopMembersGenerateGroupReportRequest.RequestSchema |
| 75 | + { |
| 76 | + Unit = ReportUnit.Words, |
| 77 | + LanguageId = "uk", |
| 78 | + Format = ReportFormat.Xlsx |
| 79 | + } |
| 80 | + }; |
| 81 | + |
| 82 | + Mock<ICrowdinApiClient> mockClient = TestUtils.CreateMockClientWithDefaultParser(); |
| 83 | + |
| 84 | + var url = $"/groups/{groupId}/reports"; |
| 85 | + |
| 86 | + mockClient |
| 87 | + .Setup(client => client.SendPostRequest(url, request, null)) |
| 88 | + .ReturnsAsync(new CrowdinApiResult |
| 89 | + { |
| 90 | + StatusCode = HttpStatusCode.OK, |
| 91 | + JsonObject = mockResponseObject |
| 92 | + }); |
| 93 | + |
| 94 | + var executor = new ReportsApiExecutor(mockClient.Object); |
| 95 | + var response = await executor.GenerateGroupReport(groupId, request); |
| 96 | + |
| 97 | + Assert.NotNull(response); |
| 98 | + Assert.IsType<GroupReportStatus>(response); |
| 99 | + } |
| 100 | + |
| 101 | + [Fact] |
| 102 | + public async Task CheckGroupReportGenerationStatus() |
| 103 | + { |
| 104 | + Mock<ICrowdinApiClient> mockClient = TestUtils.CreateMockClientWithDefaultParser(); |
| 105 | + |
| 106 | + var url = $"/groups/{groupId}/reports/{reportId}"; |
| 107 | + |
| 108 | + mockClient |
| 109 | + .Setup(client => client.SendGetRequest(url, null)) |
| 110 | + .ReturnsAsync(new CrowdinApiResult |
| 111 | + { |
| 112 | + StatusCode = HttpStatusCode.OK, |
| 113 | + JsonObject = mockResponseObject |
| 114 | + }); |
| 115 | + |
| 116 | + var executor = new ReportsApiExecutor(mockClient.Object); |
| 117 | + var response = await executor.CheckGroupReportGenerationStatus(groupId, reportId); |
| 118 | + |
| 119 | + Assert.NotNull(response); |
| 120 | + Assert.IsType<GroupReportStatus>(response); |
| 121 | + } |
| 122 | + |
| 123 | + [Fact] |
| 124 | + public async Task DownloadGroupReport() |
| 125 | + { |
| 126 | + var mockResponseObject = JObject.Parse(@" |
| 127 | + { |
| 128 | + ""data"": { |
| 129 | + ""url"": ""https://production-enterprise-importer.downloads.crowdin.com/992000002/2/14.xliff?response-content-disposition=attachment%3B%20filename%3D%22APP.xliff%22&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIGJKLQV66ZXPMMEA%2F20190920%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20190920T093121Z&X-Amz-SignedHeaders=host&X-Amz-Expires=3600&X-Amz-Signature=439ebd69a1b7e4c23e6d17891a491c94f832e0c82e4692dedb35a6cd1e624b62"", |
| 130 | + ""expireIn"": ""2019-09-20T10:31:21+00:00"" |
| 131 | + } |
| 132 | + } |
| 133 | + "); |
| 134 | + |
| 135 | + Mock<ICrowdinApiClient> mockClient = TestUtils.CreateMockClientWithDefaultParser(); |
| 136 | + |
| 137 | + var url = $"/groups/{groupId}/reports/{reportId}/download"; |
| 138 | + |
| 139 | + mockClient |
| 140 | + .Setup(client => client.SendGetRequest(url, null)) |
| 141 | + .ReturnsAsync(new CrowdinApiResult |
| 142 | + { |
| 143 | + StatusCode = HttpStatusCode.OK, |
| 144 | + JsonObject = mockResponseObject |
| 145 | + }); |
| 146 | + |
| 147 | + var executor = new ReportsApiExecutor(mockClient.Object); |
| 148 | + var response = await executor.DownloadGroupReport(groupId, reportId); |
| 149 | + |
| 150 | + Assert.NotNull(response); |
| 151 | + Assert.IsType<DownloadLink>(response); |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments