Add AttachmentHeader to AuthorInfo

This way the UI can retrieve the author's avatar (if it exists).
This commit is contained in:
Torsten Grote
2020-11-24 11:15:34 -03:00
parent d0d2e0ed82
commit c3cea37641
14 changed files with 196 additions and 102 deletions

View File

@@ -64,12 +64,16 @@ public class AvatarManagerIntegrationTest
@Test
public void testAddingAndSyncAvatars() throws Exception {
// Both contacts don't have avatars
assertNull(avatarManager0.getMyAvatarHeader());
assertNull(avatarManager1.getMyAvatarHeader());
assertNull(db0.transactionWithNullableResult(true,
txn -> avatarManager0.getMyAvatarHeader(txn)));
assertNull(db1.transactionWithNullableResult(true,
txn -> avatarManager1.getMyAvatarHeader(txn)));
// Both contacts don't see avatars for each other
assertNull(avatarManager0.getAvatarHeader(contact1From0));
assertNull(avatarManager1.getAvatarHeader(contact0From1));
assertNull(db0.transactionWithNullableResult(true,
txn -> avatarManager0.getAvatarHeader(txn, contact1From0)));
assertNull(db1.transactionWithNullableResult(true,
txn -> avatarManager1.getAvatarHeader(txn, contact0From1)));
// 0 adds avatar
byte[] avatar0bytes = getRandomBytes(42);
@@ -79,7 +83,8 @@ public class AvatarManagerIntegrationTest
assertEquals(contentType, header0.getContentType());
// 0 sees their own avatar
header0 = avatarManager0.getMyAvatarHeader();
header0 = db0.transactionWithResult(true,
txn -> avatarManager0.getMyAvatarHeader(txn));
assertNotNull(header0);
assertEquals(contentType, header0.getContentType());
assertNotNull(header0.getMessageId());
@@ -93,8 +98,8 @@ public class AvatarManagerIntegrationTest
sync0To1(1, true);
// 1 also sees 0's avatar now
AttachmentHeader header0From1 =
avatarManager1.getAvatarHeader(contact0From1);
AttachmentHeader header0From1 = db1.transactionWithResult(true,
txn -> avatarManager1.getAvatarHeader(txn, contact0From1));
assertNotNull(header0From1);
assertEquals(contentType, header0From1.getContentType());
assertNotNull(header0From1.getMessageId());
@@ -115,8 +120,8 @@ public class AvatarManagerIntegrationTest
sync1To0(1, true);
// 0 sees 1's avatar now
AttachmentHeader header1From0 =
avatarManager0.getAvatarHeader(contact1From0);
AttachmentHeader header1From0 = db0.transactionWithResult(true,
txn -> avatarManager0.getAvatarHeader(txn, contact1From0));
assertNotNull(header1From0);
assertEquals(contentType1, header1From0.getContentType());
assertNotNull(header1From0.getMessageId());
@@ -136,7 +141,8 @@ public class AvatarManagerIntegrationTest
avatarManager0.addAvatar(contentType, avatar0inputStream);
// 0 can retrieve their own avatar
AttachmentHeader header0 = avatarManager0.getMyAvatarHeader();
AttachmentHeader header0 = db0.transactionWithResult(true,
txn -> avatarManager0.getMyAvatarHeader(txn));
assertNotNull(header0);
Attachment attachment0 = avatarManager0.getAvatar(header0);
assertStreamMatches(avatar0bytes, attachment0.getStream());
@@ -145,8 +151,8 @@ public class AvatarManagerIntegrationTest
sync0To1(1, true);
// 1 only sees 0's avatar
AttachmentHeader header0From1 =
avatarManager1.getAvatarHeader(contact0From1);
AttachmentHeader header0From1 = db1.transactionWithNullableResult(true,
txn -> avatarManager1.getAvatarHeader(txn, contact0From1));
assertNotNull(header0From1);
Attachment attachment0From1 = avatarManager1.getAvatar(header0From1);
assertStreamMatches(avatar0bytes, attachment0From1.getStream());
@@ -158,7 +164,8 @@ public class AvatarManagerIntegrationTest
avatarManager0.addAvatar(contentType, avatar0inputStream2);
// 0 now only sees their new avatar
header0 = avatarManager0.getMyAvatarHeader();
header0 = db0.transactionWithResult(true,
txn -> avatarManager0.getMyAvatarHeader(txn));
assertNotNull(header0);
attachment0 = avatarManager0.getAvatar(header0);
assertStreamMatches(avatar0bytes2, attachment0.getStream());
@@ -167,8 +174,8 @@ public class AvatarManagerIntegrationTest
sync0To1(1, true);
// 1 only sees 0's new avatar
header0From1 =
avatarManager1.getAvatarHeader(contact0From1);
header0From1 = db1.transactionWithNullableResult(true,
txn -> avatarManager1.getAvatarHeader(txn, contact0From1));
assertNotNull(header0From1);
attachment0From1 = avatarManager1.getAvatar(header0From1);
assertStreamMatches(avatar0bytes2, attachment0From1.getStream());

View File

@@ -8,6 +8,7 @@ import org.briarproject.bramble.test.BrambleCoreIntegrationTestModule;
import org.briarproject.bramble.test.TestSocksModule;
import org.briarproject.briar.api.blog.BlogManager;
import org.briarproject.briar.api.feed.FeedManager;
import org.briarproject.briar.avatar.AvatarModule;
import org.briarproject.briar.blog.BlogModule;
import org.briarproject.briar.client.BriarClientModule;
import org.briarproject.briar.identity.IdentityModule;
@@ -21,6 +22,7 @@ import dagger.Component;
@Component(modules = {
BrambleCoreIntegrationTestModule.class,
BrambleCoreModule.class,
AvatarModule.class,
BlogModule.class,
BriarClientModule.class,
FeedModule.class,
@@ -33,6 +35,8 @@ interface FeedManagerIntegrationTestComponent
void inject(FeedManagerIntegrationTest testCase);
void inject(AvatarModule.EagerSingletons init);
void inject(BlogModule.EagerSingletons init);
void inject(FeedModule.EagerSingletons init);
@@ -51,6 +55,7 @@ interface FeedManagerIntegrationTestComponent
FeedManagerIntegrationTestComponent c) {
BrambleCoreIntegrationTestEagerSingletons.Helper
.injectEagerSingletons(c);
c.inject(new AvatarModule.EagerSingletons());
c.inject(new BlogModule.EagerSingletons());
c.inject(new FeedModule.EagerSingletons());
}

View File

@@ -8,9 +8,12 @@ import org.briarproject.bramble.api.identity.Author;
import org.briarproject.bramble.api.identity.AuthorId;
import org.briarproject.bramble.api.identity.IdentityManager;
import org.briarproject.bramble.api.identity.LocalAuthor;
import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.bramble.test.BrambleMockTestCase;
import org.briarproject.bramble.test.DbExpectations;
import org.briarproject.briar.api.avatar.AvatarManager;
import org.briarproject.briar.api.identity.AuthorInfo;
import org.briarproject.briar.api.media.AttachmentHeader;
import org.jmock.Expectations;
import org.junit.Test;
@@ -21,10 +24,13 @@ import static java.util.Collections.singletonList;
import static org.briarproject.bramble.test.TestUtils.getAuthor;
import static org.briarproject.bramble.test.TestUtils.getContact;
import static org.briarproject.bramble.test.TestUtils.getLocalAuthor;
import static org.briarproject.bramble.test.TestUtils.getRandomId;
import static org.briarproject.bramble.util.StringUtils.getRandomString;
import static org.briarproject.briar.api.identity.AuthorInfo.Status.OURSELVES;
import static org.briarproject.briar.api.identity.AuthorInfo.Status.UNKNOWN;
import static org.briarproject.briar.api.identity.AuthorInfo.Status.UNVERIFIED;
import static org.briarproject.briar.api.identity.AuthorInfo.Status.VERIFIED;
import static org.briarproject.briar.api.messaging.MessagingConstants.MAX_CONTENT_TYPE_BYTES;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
@@ -33,73 +39,87 @@ public class AuthorManagerImplTest extends BrambleMockTestCase {
private final DatabaseComponent db = context.mock(DatabaseComponent.class);
private final IdentityManager identityManager =
context.mock(IdentityManager.class);
private final AvatarManager avatarManager =
context.mock(AvatarManager.class);
private final Author remote = getAuthor();
private final LocalAuthor localAuthor = getLocalAuthor();
private final AuthorId local = localAuthor.getId();
private final boolean verified = false;
private final Contact contact = getContact(remote, local, verified);
private final String contentType = getRandomString(MAX_CONTENT_TYPE_BYTES);
private final AttachmentHeader avatarHeader =
new AttachmentHeader(new MessageId(getRandomId()), contentType);
private final AuthorManagerImpl authorManager =
new AuthorManagerImpl(db, identityManager);
new AuthorManagerImpl(db, identityManager, avatarManager);
@Test
public void testGetAuthorInfo() throws Exception {
public void testGetAuthorInfoUnverified() throws Exception {
Transaction txn = new Transaction(null, true);
checkAuthorInfoContext(txn, remote.getId(), singletonList(contact));
context.checking(new DbExpectations() {{
oneOf(identityManager).getLocalAuthor(txn);
will(returnValue(localAuthor));
oneOf(db).getContactsByAuthorId(txn, remote.getId());
will(returnValue(singletonList(contact)));
oneOf(avatarManager).getAvatarHeader(txn, contact);
will(returnValue(avatarHeader));
}});
AuthorInfo authorInfo =
authorManager.getAuthorInfo(txn, remote.getId());
assertEquals(UNVERIFIED, authorInfo.getStatus());
assertEquals(contact.getAlias(), authorInfo.getAlias());
assertEquals(avatarHeader, authorInfo.getAvatarHeader());
}
@Test
public void testGetAuthorInfoTransaction() throws DbException {
public void testGetAuthorInfoUnknown() throws DbException {
Transaction txn = new Transaction(null, true);
// check unknown author
context.checking(new Expectations() {{
oneOf(identityManager).getLocalAuthor(txn);
will(returnValue(localAuthor));
oneOf(db).getContactsByAuthorId(txn, remote.getId());
will(returnValue(emptyList()));
}});
checkAuthorInfoContext(txn, remote.getId(), emptyList());
AuthorInfo authorInfo =
authorManager.getAuthorInfo(txn, remote.getId());
assertEquals(UNKNOWN, authorInfo.getStatus());
assertNull(authorInfo.getAlias());
assertNull(authorInfo.getAvatarHeader());
}
// check unverified contact
checkAuthorInfoContext(txn, remote.getId(), singletonList(contact));
authorInfo = authorManager.getAuthorInfo(txn, remote.getId());
assertEquals(UNVERIFIED, authorInfo.getStatus());
assertEquals(contact.getAlias(), authorInfo.getAlias());
@Test
public void testGetAuthorInfoVerified() throws DbException {
Transaction txn = new Transaction(null, true);
// check verified contact
Contact verified = getContact(remote, local, true);
checkAuthorInfoContext(txn, remote.getId(), singletonList(verified));
authorInfo = authorManager.getAuthorInfo(txn, remote.getId());
context.checking(new DbExpectations() {{
oneOf(avatarManager).getAvatarHeader(txn, verified);
will(returnValue(avatarHeader));
}});
AuthorInfo authorInfo =
authorManager.getAuthorInfo(txn, remote.getId());
assertEquals(VERIFIED, authorInfo.getStatus());
assertEquals(verified.getAlias(), authorInfo.getAlias());
assertEquals(avatarHeader, authorInfo.getAvatarHeader());
}
@Test
public void testGetAuthorInfoOurselves() throws DbException {
Transaction txn = new Transaction(null, true);
// check ourselves
context.checking(new Expectations() {{
oneOf(identityManager).getLocalAuthor(txn);
will(returnValue(localAuthor));
never(db).getContactsByAuthorId(txn, remote.getId());
oneOf(avatarManager).getMyAvatarHeader(txn);
will(returnValue(avatarHeader));
}});
authorInfo = authorManager.getAuthorInfo(txn, localAuthor.getId());
AuthorInfo authorInfo =
authorManager.getAuthorInfo(txn, localAuthor.getId());
assertEquals(OURSELVES, authorInfo.getStatus());
assertNull(authorInfo.getAlias());
assertEquals(avatarHeader, authorInfo.getAvatarHeader());
}
private void checkAuthorInfoContext(Transaction txn, AuthorId authorId,

View File

@@ -3,6 +3,7 @@ package org.briarproject.briar.messaging;
import org.briarproject.bramble.BrambleCoreIntegrationTestEagerSingletons;
import org.briarproject.bramble.BrambleCoreModule;
import org.briarproject.bramble.test.BrambleCoreIntegrationTestModule;
import org.briarproject.briar.avatar.AvatarModule;
import org.briarproject.briar.client.BriarClientModule;
import org.briarproject.briar.forum.ForumModule;
import org.briarproject.briar.identity.IdentityModule;
@@ -16,6 +17,7 @@ import dagger.Component;
BrambleCoreIntegrationTestModule.class,
BrambleCoreModule.class,
BriarClientModule.class,
AvatarModule.class,
ForumModule.class,
IdentityModule.class,
MessagingModule.class
@@ -25,6 +27,8 @@ interface MessageSizeIntegrationTestComponent
void inject(MessageSizeIntegrationTest testCase);
void inject(AvatarModule.EagerSingletons init);
void inject(ForumModule.EagerSingletons init);
void inject(MessagingModule.EagerSingletons init);
@@ -35,6 +39,7 @@ interface MessageSizeIntegrationTestComponent
MessageSizeIntegrationTestComponent c) {
BrambleCoreIntegrationTestEagerSingletons.Helper
.injectEagerSingletons(c);
c.inject(new AvatarModule.EagerSingletons());
c.inject(new ForumModule.EagerSingletons());
c.inject(new MessagingModule.EagerSingletons());
}