mirror of
https://github.com/bitwarden/server.git
synced 2025-04-16 02:28:13 -05:00
Add UpdateCiphersAsync Test (#5543)
* Add UpdateCiphersAsync Test * Fix UpdateCiphersAsync * Fix #2 * Fix SQL Server * Formatting
This commit is contained in:
parent
dff00e613d
commit
bfe5ecda92
@ -711,7 +711,7 @@ public class CipherRepository : Repository<Cipher, Guid>, ICipherRepository
|
||||
row[creationDateColumn] = cipher.CreationDate;
|
||||
row[revisionDateColumn] = cipher.RevisionDate;
|
||||
row[deletedDateColumn] = cipher.DeletedDate.HasValue ? (object)cipher.DeletedDate : DBNull.Value;
|
||||
row[repromptColumn] = cipher.Reprompt;
|
||||
row[repromptColumn] = cipher.Reprompt.HasValue ? cipher.Reprompt.Value : DBNull.Value;
|
||||
row[keyColummn] = cipher.Key;
|
||||
|
||||
ciphersTable.Rows.Add(row);
|
||||
|
@ -863,8 +863,30 @@ public class CipherRepository : Repository<Core.Vault.Entities.Cipher, Cipher, G
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var entities = Mapper.Map<List<Cipher>>(ciphers);
|
||||
await dbContext.BulkCopyAsync(base.DefaultBulkCopyOptions, entities);
|
||||
var ciphersToUpdate = ciphers.ToDictionary(c => c.Id);
|
||||
|
||||
var existingCiphers = await dbContext.Ciphers
|
||||
.Where(c => c.UserId == userId && ciphersToUpdate.Keys.Contains(c.Id))
|
||||
.ToDictionaryAsync(c => c.Id);
|
||||
|
||||
foreach (var (cipherId, cipher) in ciphersToUpdate)
|
||||
{
|
||||
if (!existingCiphers.TryGetValue(cipherId, out var existingCipher))
|
||||
{
|
||||
// The Dapper version does not validate that the same amount of items given where updated.
|
||||
continue;
|
||||
}
|
||||
|
||||
existingCipher.UserId = cipher.UserId;
|
||||
existingCipher.OrganizationId = cipher.OrganizationId;
|
||||
existingCipher.Type = cipher.Type;
|
||||
existingCipher.Data = cipher.Data;
|
||||
existingCipher.Attachments = cipher.Attachments;
|
||||
existingCipher.RevisionDate = cipher.RevisionDate;
|
||||
existingCipher.DeletedDate = cipher.DeletedDate;
|
||||
existingCipher.Key = cipher.Key;
|
||||
}
|
||||
|
||||
await dbContext.UserBumpAccountRevisionDateAsync(userId);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
|
@ -883,4 +883,33 @@ public class CipherRepositoryTests
|
||||
Assert.Contains(user2TaskCiphers, t => t.CipherId == manageCipher1.Id && t.TaskId == securityTasks[0].Id);
|
||||
Assert.Contains(user2TaskCiphers, t => t.CipherId == manageCipher2.Id && t.TaskId == securityTasks[1].Id);
|
||||
}
|
||||
|
||||
[DatabaseTheory, DatabaseData]
|
||||
public async Task UpdateCiphersAsync_Works(ICipherRepository cipherRepository, IUserRepository userRepository)
|
||||
{
|
||||
var user = await userRepository.CreateAsync(new User
|
||||
{
|
||||
Name = "Test User",
|
||||
Email = $"test+{Guid.NewGuid()}@email.com",
|
||||
ApiKey = "TEST",
|
||||
SecurityStamp = "stamp",
|
||||
});
|
||||
|
||||
var cipher1 = await CreatePersonalCipher(user, cipherRepository);
|
||||
var cipher2 = await CreatePersonalCipher(user, cipherRepository);
|
||||
|
||||
cipher1.Type = CipherType.SecureNote;
|
||||
cipher2.Attachments = "new_attachments";
|
||||
|
||||
await cipherRepository.UpdateCiphersAsync(user.Id, [cipher1, cipher2]);
|
||||
|
||||
var updatedCipher1 = await cipherRepository.GetByIdAsync(cipher1.Id);
|
||||
var updatedCipher2 = await cipherRepository.GetByIdAsync(cipher2.Id);
|
||||
|
||||
Assert.NotNull(updatedCipher1);
|
||||
Assert.NotNull(updatedCipher2);
|
||||
|
||||
Assert.Equal(CipherType.SecureNote, updatedCipher1.Type);
|
||||
Assert.Equal("new_attachments", updatedCipher2.Attachments);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user