1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-26 12:55:17 +01:00
bitwarden-server/test/Core.Test/Services/CollectionServiceTests.cs

185 lines
6.8 KiB
C#
Raw Normal View History

using System;
using System.Threading.Tasks;
using Xunit;
using Bit.Core.Repositories;
using Bit.Core.Services;
using NSubstitute;
using Bit.Core.Exceptions;
namespace Bit.Core.Test.Services
{
public class CollectionServiceTest
{
2019-02-10 04:05:24 +01:00
private readonly IEventService _eventService;
private readonly IOrganizationRepository _organizationRepository;
private readonly IOrganizationUserRepository _organizationUserRepository;
private readonly ICollectionRepository _collectionRepository;
private readonly IUserRepository _userRepository;
private readonly IMailService _mailService;
public CollectionServiceTest()
{
_eventService = Substitute.For<IEventService>();
_organizationRepository = Substitute.For<IOrganizationRepository>();
_organizationUserRepository = Substitute.For<IOrganizationUserRepository>();
_collectionRepository = Substitute.For<ICollectionRepository>();
_userRepository = Substitute.For<IUserRepository>();
_mailService = Substitute.For<IMailService>();
}
[Fact]
public async Task SaveAsync_CollectionNotFound()
{
2019-02-10 04:05:24 +01:00
var collectionService = new CollectionService(
_eventService,
_organizationRepository,
_organizationUserRepository,
_collectionRepository,
_userRepository,
_mailService);
var id = Guid.NewGuid();
var collection = new Models.Table.Collection
{
Id = id,
};
var ex = await Assert.ThrowsAsync<BadRequestException>(() => collectionService.SaveAsync(collection));
2019-03-02 21:28:32 +01:00
Assert.Equal("Organization not found", ex.Message);
}
[Fact]
2019-02-10 04:05:24 +01:00
public async Task SaveAsync_DefaultCollectionId_CreatesCollectionInTheRepository()
{
// prepare the organization
var testOrganizationId = Guid.NewGuid();
2019-02-10 04:05:24 +01:00
var testOrganization = new Models.Table.Organization
{
Id = testOrganizationId,
};
2019-02-10 04:05:24 +01:00
_organizationRepository.GetByIdAsync(testOrganizationId).Returns(testOrganization);
2019-02-10 04:05:24 +01:00
var collectionService = new CollectionService(
_eventService,
_organizationRepository,
_organizationUserRepository,
_collectionRepository,
_userRepository,
_mailService);
// execute
var testCollection = new Models.Table.Collection
{
OrganizationId = testOrganizationId,
};
await collectionService.SaveAsync(testCollection);
// verify
await _collectionRepository.Received().CreateAsync(testCollection);
}
[Fact]
2019-02-10 04:05:24 +01:00
public async Task SaveAsync_RespectsMaxNumberOfCollectionsPerOrganization()
{
// prepare the organization
var testOrganizationId = Guid.NewGuid();
2019-02-10 04:05:24 +01:00
var testOrganization = new Models.Table.Organization
{
Id = testOrganizationId,
MaxCollections = 2,
};
_organizationRepository.GetByIdAsync(testOrganizationId).Returns(testOrganization);
_collectionRepository.GetCountByOrganizationIdAsync(testOrganizationId).Returns(2);
// execute
2019-02-10 04:05:24 +01:00
var collectionService = new CollectionService(
_eventService,
_organizationRepository,
_organizationUserRepository,
_collectionRepository,
_userRepository,
_mailService);
var testCollection = new Models.Table.Collection { OrganizationId = testOrganizationId };
// verify & expect exception to be thrown
var ex = await Assert.ThrowsAsync<BadRequestException>(() => collectionService.SaveAsync(testCollection));
Assert.Equal("You have reached the maximum number of collections (2) for this organization.",
2019-03-02 21:28:32 +01:00
ex.Message);
}
[Fact]
2019-02-10 04:05:24 +01:00
public async Task DeleteUserAsync_DeletesValidUserWhoBelongsToCollection()
{
// prepare the organization
var testOrganizationId = Guid.NewGuid();
2019-02-10 04:05:24 +01:00
var testOrganization = new Models.Table.Organization
{
Id = testOrganizationId,
};
var testUserId = Guid.NewGuid();
var organizationUser = new Models.Table.OrganizationUser
{
Id = testUserId,
OrganizationId = testOrganizationId,
};
_organizationUserRepository.GetByIdAsync(testUserId).Returns(organizationUser);
// execute
2019-02-10 04:05:24 +01:00
var collectionService = new CollectionService(
_eventService,
_organizationRepository,
_organizationUserRepository,
_collectionRepository,
_userRepository,
_mailService);
var testCollection = new Models.Table.Collection { OrganizationId = testOrganizationId };
await collectionService.DeleteUserAsync(testCollection, organizationUser.Id);
// verify
await _collectionRepository.Received().DeleteUserAsync(testCollection.Id, organizationUser.Id);
}
[Fact]
2019-02-10 04:05:24 +01:00
public async Task DeleteUserAsync_ThrowsIfUserIsInvalid()
{
// prepare the organization
var testOrganizationId = Guid.NewGuid();
2019-02-10 04:05:24 +01:00
var testOrganization = new Models.Table.Organization
{
Id = testOrganizationId,
};
var testUserId = Guid.NewGuid();
var nonOrganizationUser = new Models.Table.OrganizationUser
{
Id = testUserId,
OrganizationId = Guid.NewGuid(),
};
_organizationUserRepository.GetByIdAsync(testUserId).Returns(nonOrganizationUser);
// execute
2019-02-10 04:05:24 +01:00
var collectionService = new CollectionService(
_eventService,
_organizationRepository,
_organizationUserRepository,
_collectionRepository,
_userRepository,
_mailService);
var testCollection = new Models.Table.Collection { OrganizationId = testOrganizationId };
// verify
// invalid user
2019-02-10 04:05:24 +01:00
await Assert.ThrowsAsync<NotFoundException>(() =>
collectionService.DeleteUserAsync(testCollection, Guid.NewGuid()));
// user from other organization
2019-02-10 04:05:24 +01:00
await Assert.ThrowsAsync<NotFoundException>(() =>
collectionService.DeleteUserAsync(testCollection, testUserId));
}
}
}