FactoryEditValidatorImpl implements FactoryEditValidator { @Override public void validate(Factory factory, String userId) throws ApiException { boolean granted = validateAuthor(factory, userId); if (!granted) { validateAccountOwner(factory, userId); } }  @Override void validate(Factory factory, String userId);  }
@Test(expectedExceptions = ServerException.class) public void testNoAuthor() throws ApiException { factoryEditValidator.validate(factory, ""); }
@Test public void testUserIsTheAuthor() throws ApiException { String userId = "florent"; Author author = mock(Author.class); doReturn(author).when(factory).getCreator(); doReturn(userId).when(author).getUserId(); factoryEditValidator.validate(factory, userId); }
@Test(expectedExceptions = ForbiddenException.class) public void testWithoutAccountID() throws ApiException { String userIdFactory = "florent"; Author author = mock(Author.class); doReturn(author).when(factory).getCreator(); doReturn(userIdFactory).when(author).getUserId(); factoryEditValidator.validate(factory, "toto"); }
@Test(expectedExceptions = ForbiddenException.class) public void testUserWithNoMembersInAccountID() throws ApiException { String userIdFactory = "florent"; String accountId = "myAccount"; Author author = mock(Author.class); doReturn("123").when(factory).getId(); doReturn(author).when(factory).getCreator(); doReturn(userIdFactory).when(author).getUserId(); doReturn(accountId).when(author).getAccountId(); factoryEditValidator.validate(factory, "toto"); }
@Test(expectedExceptions = ForbiddenException.class) public void testUserNotInAccountOwner() throws ApiException { String currentUserId = "florent"; String userIdFactory = "johndoe"; String accountId = "myAccount"; Author author = mock(Author.class); doReturn("123").when(factory).getId(); doReturn(author).when(factory).getCreator(); doReturn(userIdFactory).when(author).getUserId(); doReturn(accountId).when(author).getAccountId(); Member member = mock(Member.class); doReturn(currentUserId).when(member).getUserId(); List<Member> members = new ArrayList<>(); members.add(member); doReturn(members).when(accountDao).getMembers(eq(accountId)); factoryEditValidator.validate(factory, currentUserId); }
@Test public void testUserIsAccountOwner() throws ApiException { String currentUserId = "florent"; String userIdFactory = "johndoe"; String accountId = "myAccount"; Author author = mock(Author.class); doReturn("123").when(factory).getId(); doReturn(author).when(factory).getCreator(); doReturn(userIdFactory).when(author).getUserId(); doReturn(accountId).when(author).getAccountId(); Member member = mock(Member.class); doReturn(currentUserId).when(member).getUserId(); List<String> roles = Arrays.asList("account/owner"); doReturn(roles).when(member).getRoles(); List<Member> members = new ArrayList<>(); members.add(member); doReturn(members).when(accountDao).getMembers(eq(accountId)); factoryEditValidator.validate(factory, currentUserId); }