Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed all the comment in my test and added is_active property in my update organization dto #444

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,13 @@ public class UpdateOrganizationShould

public UpdateOrganizationShould()
{
// Setup mapper with proper profile
var mappingProfile = new OrganizationMapperProfile();
var configuration = new MapperConfiguration(cfg => cfg.AddProfile(mappingProfile));
_mapper = new Mapper(configuration);

// Setup repository and authentication mocks
_repositoryMock = new Mock<IRepository<Domain.Entities.Organization>>();
_authenticationServiceMock = new Mock<IAuthenticationService>();

// Initialize the handler with mocks
_handler = new UpdateOrganizationCommandHandler(
_repositoryMock.Object,
_mapper,
Expand All @@ -41,7 +38,6 @@ public UpdateOrganizationShould()
[Fact]
public async Task Handle_ShouldReturn200_WhenOrganizationUpdatedSuccessfully()
{
// Arrange
var userId = Guid.NewGuid();
var organizationId = Guid.NewGuid();

Expand All @@ -54,7 +50,8 @@ public async Task Handle_ShouldReturn200_WhenOrganizationUpdatedSuccessfully()
Type = "Enterprise",
Country = "Updated Country",
Address = "Updated Address",
State = "Updated State"
State = "Updated State",
IsActive = true,
};

var existingOrganization = new Domain.Entities.Organization
Expand All @@ -68,41 +65,35 @@ public async Task Handle_ShouldReturn200_WhenOrganizationUpdatedSuccessfully()
Type = "Old Type",
Country = "Old Country",
Address = "Old Address",
State = "Old State"
State = "Old State",
IsActive = false,
};

// Setup authentication to return the current user
_authenticationServiceMock
.Setup(a => a.GetCurrentUserAsync())
.ReturnsAsync(userId);

// Setup repository to return existing organization when GetBySpec is called
_repositoryMock
.Setup(r => r.GetBySpec(It.IsAny<Expression<Func<Domain.Entities.Organization, bool>>>()))
.ReturnsAsync(existingOrganization);

// Setup UpdateAsync to do nothing (void return)
_repositoryMock
.Setup(r => r.UpdateAsync(It.IsAny<Domain.Entities.Organization>()))
.Returns(Task.CompletedTask);

// Setup SaveChanges to return success
_repositoryMock
.Setup(r => r.SaveChanges())
.Returns(Task.FromResult(true));

var command = new UpdateOrganizationCommand(organizationId, updateDto);

// Act
var result = await _handler.Handle(command, CancellationToken.None);

// Assert
Assert.NotNull(result);
Assert.Equal(StatusCodes.Status200OK, result.StatusCode);
Assert.Equal("Organization updated successfully", result.Message);
Assert.NotNull(result.Data);

// Verify all properties were mapped correctly
Assert.Equal(updateDto.Name, result.Data.Name);
Assert.Equal(updateDto.Description, result.Data.Description);
Assert.Equal(updateDto.Email, result.Data.Email);
Expand All @@ -111,71 +102,60 @@ public async Task Handle_ShouldReturn200_WhenOrganizationUpdatedSuccessfully()
Assert.Equal(updateDto.Country, result.Data.Country);
Assert.Equal(updateDto.Address, result.Data.Address);
Assert.Equal(updateDto.State, result.Data.State);
Assert.Equal(updateDto.IsActive, result.Data.IsActive);

// Verify repository methods were called
_repositoryMock.Verify(r => r.UpdateAsync(It.IsAny<Domain.Entities.Organization>()), Times.Once);
_repositoryMock.Verify(r => r.SaveChanges(), Times.Once);
}

[Fact]
public async Task Handle_ShouldReturn404_WhenOrganizationNotFound()
{
// Arrange
var organizationId = Guid.NewGuid();
var updateDto = new UpdateOrganizationDto { Name = "Updated Org" };
var command = new UpdateOrganizationCommand(organizationId, updateDto);

// Setup repository to return null (organization not found)
_repositoryMock
.Setup(r => r.GetBySpec(It.IsAny<Expression<Func<Domain.Entities.Organization, bool>>>()))
.ReturnsAsync((Domain.Entities.Organization)null);

// Act
var result = await _handler.Handle(command, CancellationToken.None);

// Assert
Assert.Equal(StatusCodes.Status404NotFound, result.StatusCode);
Assert.Equal("Organization not found", result.Message);

// Verify Update/Save were never called
_repositoryMock.Verify(r => r.UpdateAsync(It.IsAny<Domain.Entities.Organization>()), Times.Never);
_repositoryMock.Verify(r => r.SaveChanges(), Times.Never);
}

[Fact]
public async Task Handle_ShouldReturn403_WhenUnauthorizedUpdateAttempt()
{
// Arrange
var organizationId = Guid.NewGuid();
var ownerId = Guid.NewGuid();
var unauthorizedUserId = Guid.NewGuid(); // Different from owner
var unauthorizedUserId = Guid.NewGuid();
var updateDto = new UpdateOrganizationDto { Name = "Updated Org" };
var command = new UpdateOrganizationCommand(organizationId, updateDto);

var organization = new Domain.Entities.Organization
{
Id = organizationId,
OwnerId = ownerId // This is the owner
OwnerId = ownerId
};

// Setup authentication to return unauthorized user
_authenticationServiceMock
.Setup(a => a.GetCurrentUserAsync())
.ReturnsAsync(unauthorizedUserId); // Different from owner
.ReturnsAsync(unauthorizedUserId);

// Setup repository to return the organization
_repositoryMock
.Setup(r => r.GetBySpec(It.IsAny<Expression<Func<Domain.Entities.Organization, bool>>>()))
.ReturnsAsync(organization);

// Act
var result = await _handler.Handle(command, CancellationToken.None);

// Assert
Assert.Equal(StatusCodes.Status403Forbidden, result.StatusCode);
Assert.Equal("You are not authorized to update this organization", result.Message);

// Verify Update/Save were never called
_repositoryMock.Verify(r => r.UpdateAsync(It.IsAny<Domain.Entities.Organization>()), Times.Never);
_repositoryMock.Verify(r => r.SaveChanges(), Times.Never);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ public class UpdateOrganizationDto

[JsonPropertyName("state")]
public string State { get; set; }
[JsonPropertyName("is_active")]
public bool IsActive { get; set; }
}
}
10 changes: 5 additions & 5 deletions src/Hng.Web/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
},
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnectionString": "Host=localhost; Port=5432; Database=boiler_plate_db; Username=postgres; Password=\"Test123$\";",
"RedisConnectionString": "localhost:6379"
"DefaultConnectionString": "",
"RedisConnectionString": ""

},
"Jwt": {
"SecretKey": "V@7y$#z9Gq!Np3X2rD6&K*B5wLm%+T8aJ4fH",
"ExpireInMinute": "15"
"SecretKey": "",
"ExpireInMinute": ""
},
"PaystackApiKeys": {
"Endpoint": "https://api.paystack.co/",
Expand All @@ -22,7 +22,7 @@
},
"SmtpCredentials": {
"Host": "",
"Port": 587,
"Port": "",
"Username": "",
"Password": ""
},
Expand Down
Loading