|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using Microting.eFormApi.BasePn.Infrastructure.Models.API; |
| 5 | +using NSubstitute; |
| 6 | +using NUnit.Framework; |
| 7 | +using TimePlanning.Pn.Grpc; |
| 8 | +using TimePlanning.Pn.Services.GrpcServices; |
| 9 | +using TimePlanning.Pn.Services.TimePlanningSettingService; |
| 10 | +using TimePlanning.Pn.Test.Helpers; |
| 11 | + |
| 12 | +namespace TimePlanning.Pn.Test.GrpcServices; |
| 13 | + |
| 14 | +[TestFixture] |
| 15 | +public class TimePlanningSettingsGrpcServiceTests |
| 16 | +{ |
| 17 | + private ISettingService _settingService; |
| 18 | + private TimePlanningSettingsGrpcService _grpcService; |
| 19 | + |
| 20 | + [SetUp] |
| 21 | + public void SetUp() |
| 22 | + { |
| 23 | + _settingService = Substitute.For<ISettingService>(); |
| 24 | + _grpcService = new TimePlanningSettingsGrpcService(_settingService); |
| 25 | + } |
| 26 | + |
| 27 | + [Test] |
| 28 | + public async Task GetRegistrationSitesByCurrentUser_Success_MapsSitesToGrpcResponse() |
| 29 | + { |
| 30 | + var sites = new List<Infrastructure.Models.Settings.Site> |
| 31 | + { |
| 32 | + new() |
| 33 | + { |
| 34 | + SiteId = 1, |
| 35 | + SiteName = "Site Alpha", |
| 36 | + FirstName = "John", |
| 37 | + LastName = "Doe", |
| 38 | + Email = "john@example.com", |
| 39 | + PinCode = "1234", |
| 40 | + DefaultLanguage = "en", |
| 41 | + CustomerNo = 42, |
| 42 | + OtpCode = 100, |
| 43 | + UnitId = 10, |
| 44 | + WorkerUid = 200, |
| 45 | + HoursStarted = true, |
| 46 | + PauseStarted = false, |
| 47 | + AutoBreakCalculationActive = true, |
| 48 | + ThirdShiftActive = true, |
| 49 | + FourthShiftActive = false, |
| 50 | + FifthShiftActive = true, |
| 51 | + SnapshotEnabled = false, |
| 52 | + Resigned = false, |
| 53 | + ResignedAtDate = DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc), |
| 54 | + AvatarUrl = "https://example.com/avatar1.png", |
| 55 | + PhoneNumber = "+4512345678", |
| 56 | + }, |
| 57 | + new() |
| 58 | + { |
| 59 | + SiteId = 2, |
| 60 | + SiteName = "Site Beta", |
| 61 | + FirstName = "Jane", |
| 62 | + LastName = "Smith", |
| 63 | + Email = "jane@example.com", |
| 64 | + PinCode = "5678", |
| 65 | + DefaultLanguage = "da", |
| 66 | + CustomerNo = 99, |
| 67 | + OtpCode = 200, |
| 68 | + UnitId = 20, |
| 69 | + WorkerUid = 300, |
| 70 | + HoursStarted = false, |
| 71 | + PauseStarted = true, |
| 72 | + AutoBreakCalculationActive = false, |
| 73 | + ThirdShiftActive = false, |
| 74 | + FourthShiftActive = true, |
| 75 | + FifthShiftActive = false, |
| 76 | + SnapshotEnabled = true, |
| 77 | + Resigned = true, |
| 78 | + ResignedAtDate = new DateTime(2024, 1, 1, 0, 0, 0, DateTimeKind.Utc), |
| 79 | + AvatarUrl = "https://example.com/avatar2.png", |
| 80 | + PhoneNumber = "+4587654321", |
| 81 | + }, |
| 82 | + }; |
| 83 | + |
| 84 | + _settingService.GetAvailableSitesByCurrentUser() |
| 85 | + .Returns(new OperationDataResult<List<Infrastructure.Models.Settings.Site>>( |
| 86 | + true, "OK", sites)); |
| 87 | + |
| 88 | + var request = new GetRegistrationSitesByCurrentUserRequest(); |
| 89 | + |
| 90 | + var response = await _grpcService.GetRegistrationSitesByCurrentUser( |
| 91 | + request, TestServerCallContextFactory.Create()); |
| 92 | + |
| 93 | + Assert.That(response.Success, Is.True); |
| 94 | + Assert.That(response.Message, Is.EqualTo("OK")); |
| 95 | + Assert.That(response.Model, Has.Count.EqualTo(2)); |
| 96 | + |
| 97 | + var site1 = response.Model[0]; |
| 98 | + Assert.That(site1.SiteId, Is.EqualTo(1)); |
| 99 | + Assert.That(site1.SiteName, Is.EqualTo("Site Alpha")); |
| 100 | + Assert.That(site1.FirstName, Is.EqualTo("John")); |
| 101 | + Assert.That(site1.LastName, Is.EqualTo("Doe")); |
| 102 | + Assert.That(site1.Email, Is.EqualTo("john@example.com")); |
| 103 | + Assert.That(site1.PinCode, Is.EqualTo("1234")); |
| 104 | + Assert.That(site1.DefaultLanguage, Is.EqualTo("en")); |
| 105 | + Assert.That(site1.CustomerNo, Is.EqualTo(42)); |
| 106 | + Assert.That(site1.OtpCode, Is.EqualTo(100)); |
| 107 | + Assert.That(site1.UnitId, Is.EqualTo(10)); |
| 108 | + Assert.That(site1.WorkerUid, Is.EqualTo(200)); |
| 109 | + Assert.That(site1.HoursStarted, Is.True); |
| 110 | + Assert.That(site1.PauseStarted, Is.False); |
| 111 | + Assert.That(site1.AutoBreakCalculationActive, Is.True); |
| 112 | + Assert.That(site1.ThirdShiftActive, Is.True); |
| 113 | + Assert.That(site1.FourthShiftActive, Is.False); |
| 114 | + Assert.That(site1.FifthShiftActive, Is.True); |
| 115 | + Assert.That(site1.SnapshotEnabled, Is.False); |
| 116 | + Assert.That(site1.Resigned, Is.False); |
| 117 | + Assert.That(site1.AvatarUrl, Is.EqualTo("https://example.com/avatar1.png")); |
| 118 | + Assert.That(site1.PhoneNumber, Is.EqualTo("+4512345678")); |
| 119 | + |
| 120 | + var site2 = response.Model[1]; |
| 121 | + Assert.That(site2.SiteId, Is.EqualTo(2)); |
| 122 | + Assert.That(site2.SiteName, Is.EqualTo("Site Beta")); |
| 123 | + Assert.That(site2.FirstName, Is.EqualTo("Jane")); |
| 124 | + Assert.That(site2.LastName, Is.EqualTo("Smith")); |
| 125 | + Assert.That(site2.Email, Is.EqualTo("jane@example.com")); |
| 126 | + Assert.That(site2.PinCode, Is.EqualTo("5678")); |
| 127 | + Assert.That(site2.DefaultLanguage, Is.EqualTo("da")); |
| 128 | + Assert.That(site2.CustomerNo, Is.EqualTo(99)); |
| 129 | + Assert.That(site2.OtpCode, Is.EqualTo(200)); |
| 130 | + Assert.That(site2.UnitId, Is.EqualTo(20)); |
| 131 | + Assert.That(site2.WorkerUid, Is.EqualTo(300)); |
| 132 | + Assert.That(site2.HoursStarted, Is.False); |
| 133 | + Assert.That(site2.PauseStarted, Is.True); |
| 134 | + Assert.That(site2.AutoBreakCalculationActive, Is.False); |
| 135 | + Assert.That(site2.ThirdShiftActive, Is.False); |
| 136 | + Assert.That(site2.FourthShiftActive, Is.True); |
| 137 | + Assert.That(site2.FifthShiftActive, Is.False); |
| 138 | + Assert.That(site2.SnapshotEnabled, Is.True); |
| 139 | + Assert.That(site2.Resigned, Is.True); |
| 140 | + Assert.That(site2.AvatarUrl, Is.EqualTo("https://example.com/avatar2.png")); |
| 141 | + Assert.That(site2.PhoneNumber, Is.EqualTo("+4587654321")); |
| 142 | + } |
| 143 | + |
| 144 | + [Test] |
| 145 | + public async Task GetRegistrationSitesByCurrentUser_Failure_ReturnsErrorMessage() |
| 146 | + { |
| 147 | + _settingService.GetAvailableSitesByCurrentUser() |
| 148 | + .Returns(new OperationDataResult<List<Infrastructure.Models.Settings.Site>>( |
| 149 | + false, "Access denied")); |
| 150 | + |
| 151 | + var request = new GetRegistrationSitesByCurrentUserRequest(); |
| 152 | + |
| 153 | + var response = await _grpcService.GetRegistrationSitesByCurrentUser( |
| 154 | + request, TestServerCallContextFactory.Create()); |
| 155 | + |
| 156 | + Assert.That(response.Success, Is.False); |
| 157 | + Assert.That(response.Message, Is.EqualTo("Access denied")); |
| 158 | + Assert.That(response.Model, Has.Count.EqualTo(0)); |
| 159 | + } |
| 160 | + |
| 161 | + [Test] |
| 162 | + public async Task GetRegistrationSitesByCurrentUser_EmptyList_ReturnsSuccessWithNoSites() |
| 163 | + { |
| 164 | + _settingService.GetAvailableSitesByCurrentUser() |
| 165 | + .Returns(new OperationDataResult<List<Infrastructure.Models.Settings.Site>>( |
| 166 | + true, "OK", new List<Infrastructure.Models.Settings.Site>())); |
| 167 | + |
| 168 | + var request = new GetRegistrationSitesByCurrentUserRequest(); |
| 169 | + |
| 170 | + var response = await _grpcService.GetRegistrationSitesByCurrentUser( |
| 171 | + request, TestServerCallContextFactory.Create()); |
| 172 | + |
| 173 | + Assert.That(response.Success, Is.True); |
| 174 | + Assert.That(response.Model, Has.Count.EqualTo(0)); |
| 175 | + } |
| 176 | +} |
0 commit comments