Search in sources :

Example 11 with Announcement

use of ca.corefacility.bioinformatics.irida.model.announcements.Announcement in project irida by phac-nml.

the class AnnouncementServiceImplIT method testUserMarkAnnouncementAsReadFailed.

@Test(expected = EntityExistsException.class)
@WithMockUser(username = "user", roles = "USER")
public void testUserMarkAnnouncementAsReadFailed() {
    final Announcement a = announcementService.read(1L);
    final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    final User user = userService.getUserByUsername(auth.getName());
    announcementService.markAnnouncementAsReadByUser(a, user);
}
Also used : WithMockUser(org.springframework.security.test.context.support.WithMockUser) User(ca.corefacility.bioinformatics.irida.model.user.User) Announcement(ca.corefacility.bioinformatics.irida.model.announcements.Announcement) Authentication(org.springframework.security.core.Authentication) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.Test)

Example 12 with Announcement

use of ca.corefacility.bioinformatics.irida.model.announcements.Announcement in project irida by phac-nml.

the class AnnouncementServiceImplIT method testGetUnreadAnnouncementsForUser.

@Test
@WithMockUser(username = "user3", roles = "USER")
public void testGetUnreadAnnouncementsForUser() {
    final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    final User user = userService.getUserByUsername(auth.getName());
    List<Announcement> announcementList = announcementService.getUnreadAnnouncementsForUser(user);
    assertEquals("Number of unread announcements doesn't match expected value", 6, announcementList.size());
    Announcement ann = announcementService.read(6L);
    announcementService.markAnnouncementAsReadByUser(ann, user);
    announcementList = announcementService.getUnreadAnnouncementsForUser(user);
    assertEquals("Number of unread announcements doesn't match expected value", 5, announcementList.size());
}
Also used : WithMockUser(org.springframework.security.test.context.support.WithMockUser) User(ca.corefacility.bioinformatics.irida.model.user.User) Announcement(ca.corefacility.bioinformatics.irida.model.announcements.Announcement) Authentication(org.springframework.security.core.Authentication) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.Test)

Example 13 with Announcement

use of ca.corefacility.bioinformatics.irida.model.announcements.Announcement in project irida by phac-nml.

the class AnnouncementServiceImpl method countReadsForAllAnnouncements.

/**
 * {@inheritDoc}
 */
@Override
@PreAuthorize("hasRole('ROLE_ADMIN')")
public Map<Announcement, Long> countReadsForAllAnnouncements() {
    Map<Announcement, Long> counts = new HashMap<>();
    List<Announcement> announcements = getAllAnnouncements();
    for (Announcement a : announcements) {
        counts.put(a, countReadsForOneAnnouncement(a));
    }
    return counts;
}
Also used : Announcement(ca.corefacility.bioinformatics.irida.model.announcements.Announcement) HashMap(java.util.HashMap) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 14 with Announcement

use of ca.corefacility.bioinformatics.irida.model.announcements.Announcement in project irida by phac-nml.

the class AnnouncementServiceImplIT method testGetSingleAnnouncementById.

@Test
@WithMockUser(username = "admin", roles = "ADMIN")
public void testGetSingleAnnouncementById() {
    Announcement a = announcementService.read(3L);
    Long idExpected = 3L;
    String messageExpected = "You cannot have your cake and eat it too.";
    assertEquals("IDs for announcement doesn't match", idExpected, a.getId());
    assertEquals("Announcement message content doesn't match expected", messageExpected, a.getMessage());
}
Also used : Announcement(ca.corefacility.bioinformatics.irida.model.announcements.Announcement) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.Test)

Example 15 with Announcement

use of ca.corefacility.bioinformatics.irida.model.announcements.Announcement in project irida by phac-nml.

the class AnnouncementServiceImplIT method testCreateAnnouncementAsAdmin.

@Test
@WithMockUser(username = "admin", roles = "ADMIN")
public void testCreateAnnouncementAsAdmin() {
    final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    final User user = userService.getUserByUsername(auth.getName());
    Announcement an = new Announcement("This is a message", user);
    try {
        announcementService.create(an);
    } catch (AccessDeniedException e) {
        fail("Admin should be able to create a new announcement.");
    } catch (Exception e) {
        fail("Failed for unknown reason, stack trace follows: ");
        e.printStackTrace();
    }
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) WithMockUser(org.springframework.security.test.context.support.WithMockUser) User(ca.corefacility.bioinformatics.irida.model.user.User) Announcement(ca.corefacility.bioinformatics.irida.model.announcements.Announcement) Authentication(org.springframework.security.core.Authentication) EntityExistsException(ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException) EntityNotFoundException(ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) AccessDeniedException(org.springframework.security.access.AccessDeniedException) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.Test)

Aggregations

Announcement (ca.corefacility.bioinformatics.irida.model.announcements.Announcement)29 Test (org.junit.Test)20 WithMockUser (org.springframework.security.test.context.support.WithMockUser)17 User (ca.corefacility.bioinformatics.irida.model.user.User)15 Authentication (org.springframework.security.core.Authentication)9 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)7 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)6 PageRequest (org.springframework.data.domain.PageRequest)5 DTAnnouncementUser (ca.corefacility.bioinformatics.irida.ria.web.models.datatables.DTAnnouncementUser)4 IOException (java.io.IOException)3 Sort (org.springframework.data.domain.Sort)3 ExtendedModelMap (org.springframework.ui.ExtendedModelMap)3 EntityExistsException (ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException)2 AnnouncementUserJoin (ca.corefacility.bioinformatics.irida.model.announcements.AnnouncementUserJoin)2 DataTablesResponse (ca.corefacility.bioinformatics.irida.ria.web.components.datatables.DataTablesResponse)2 DataTablesResponseModel (ca.corefacility.bioinformatics.irida.ria.web.components.datatables.models.DataTablesResponseModel)2 AnnouncementService (ca.corefacility.bioinformatics.irida.service.AnnouncementService)2 UserService (ca.corefacility.bioinformatics.irida.service.user.UserService)2 Principal (java.security.Principal)2 AccessDeniedException (org.springframework.security.access.AccessDeniedException)2