Skip to content

Commit

Permalink
[Users] Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rikurauhala committed Nov 11, 2024
1 parent 9ef0d8b commit 3c09f21
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 51 deletions.
2 changes: 1 addition & 1 deletion cypress/e2e/Course_statistics.js
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ describe('Course Statistics tests', () => {

it('Some features of Course Statistics are hidden for courseStatistics-users without other rights', () => {
cy.init('/coursestatistics', 'onlycoursestatistics')
cy.get('[data-cy=navbar-courseStatistics]').click()
cy.get('[data-cy=nav-bar-button-courseStatistics]').click()
cy.get('[data-cy=course-code-input]').type('TKT10002')
cy.contains('tr', 'TKT10002').click()
cy.contains('Filter statistics by study programmes').should('not.exist')
Expand Down
100 changes: 59 additions & 41 deletions cypress/e2e/Users.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,60 @@
/// <reference types="cypress" />

const visibleLinks = {
norights: ['University', 'Faculties', 'Special populations', 'Give feedback', 'Logout'],
onlycoursestatistics: ['University', 'Courses', 'Special populations', 'Give feedback', 'Logout'],
norights: ['University', 'Faculties', 'Special populations', 'Feedback'],
onlycoursestatistics: ['University', 'Courses', 'Special populations', 'Feedback'],
}

visibleLinks.basic = [...visibleLinks.onlycoursestatistics, 'Faculties', 'Programmes', 'Students']
visibleLinks.admin = [...visibleLinks.basic, 'Teachers', 'Users', 'Updater']
visibleLinks.admin = [...visibleLinks.basic, 'Teachers', 'Admin']

const hasVisibleItems = visibleItems => {
cy.get('[data-cy=nav-bar]').children().should('have.length', visibleItems)
}

const containsLinks = links => {
cy.get('[data-cy=nav-bar]').within(() => {
for (const link of links) {
cy.contains(link)
}
})
}

const userButtonWorks = (username, mocking = false) => {
cy.get('[data-cy=nav-bar-user-button]').click()
cy.contains(mocking ? `Mocking as ${username}` : `Logged in as ${username}`)
cy.contains('Language')
cy.contains('suomi')
cy.contains('English')
cy.contains('svenska')
cy.contains(mocking ? 'Stop mocking' : 'Log out')
}

describe('Users tests', () => {
describe('Using user with just grp-oodikone-user, no other rights', () => {
describe('Using as user with just grp-oodikone-user, no other rights', () => {
it('shows correct tabs', () => {
cy.init('', 'norights')
cy.get('[data-cy=navBar]').children().should('have.length', 7)
cy.get('[data-cy=navBar]').within(() => {
for (const link of visibleLinks.norights) {
cy.contains(link)
}
})
hasVisibleItems(7)
containsLinks(visibleLinks.norights)
userButtonWorks('norights')
})
})

describe('Using as coursestatistics user', () => {
it('shows correct tabs', () => {
cy.init('', 'onlycoursestatistics')
cy.get('[data-cy=navBar]').children().should('have.length', 7)
cy.get('[data-cy=navBar]').within(() => {
for (const link of visibleLinks.onlycoursestatistics) {
cy.contains(link)
}
})
hasVisibleItems(7)
containsLinks(visibleLinks.onlycoursestatistics)
userButtonWorks('onlycoursestatistics')
})
})

describe('Using as basic user', () => {
it('shows correct tabs', () => {
cy.init('')
cy.get('[data-cy=navBar]').children().should('have.length', 10)
cy.get('[data-cy=navBar]').within(() => {
for (const link of visibleLinks.basic) {
cy.contains(link)
}
})
hasVisibleItems(10)
containsLinks(visibleLinks.basic)
userButtonWorks('basic')
})
})

Expand All @@ -51,27 +64,32 @@ describe('Users tests', () => {
})

it('should see more stuff than others', () => {
cy.get('[data-cy=navBar]').children().should('have.length', 13)
cy.get('[data-cy=navBar]').within(() => {
for (const link of visibleLinks.admin) {
cy.contains(link)
}
})
hasVisibleItems(13)
containsLinks(visibleLinks.admin)
userButtonWorks('admin')
})

it("mocking normal user shows only the mocked user's programmes", () => {
cy.contains('mocking').should('not.exist')
cy.cs('user-edit-button-basic').click()
cy.get('i.spy').click()
cy.contains('mocking as basic')
cy.contains('Programmes').click().siblings().contains('Class statistics').click()
cy.contains('label', 'Study programme')
.siblings()
.within(() => {
cy.get("div[role='option']").should('have.length', 2)
cy.get("div[role='option']").eq(0).contains('Matemaattisten tieteiden kandiohjelma')
cy.get("div[role='option']").eq(1).contains('Matematiikan ja tilastotieteen maisteriohjelma')
})
describe('can mock as other users', () => {
beforeEach(() => {
cy.cs('user-edit-button-basic').click()
cy.get('i.spy').click()
})

it('user button shows mocked user', () => {
userButtonWorks('basic', true)
})

it("only the mocked user's programmes are visible", () => {
cy.get('[data-cy=nav-bar-button-studyProgramme]').click()
cy.get('[data-cy=nav-bar-button-class]').click()
cy.contains('label', 'Study programme')
.siblings()
.within(() => {
cy.get("div[role='option']").should('have.length', 2)
cy.get("div[role='option']").eq(0).contains('Matemaattisten tieteiden kandiohjelma')
cy.get("div[role='option']").eq(1).contains('Matematiikan ja tilastotieteen maisteriohjelma')
})
})
})
})
})
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ArrowDropDown } from '@mui/icons-material'
import { Button, Menu, MenuItem } from '@mui/material'
import { Fragment, useState } from 'react'
import { useState } from 'react'
import { Link, useLocation } from 'react-router-dom'

import { checkUserAccess, getFullStudyProgrammeRights, isDefaultServiceProvider } from '@/common'
Expand Down Expand Up @@ -80,9 +80,9 @@ export const NavigationButton = ({ item }: { item: NavigationItem }) => {

if (items) {
return (
<Fragment key={`men-item-drop-${key}`}>
<>
<Button
data-cy={`navbar-${key}`}
data-cy={`nav-bar-button-${key}`}
endIcon={<ArrowDropDown />}
onClick={event => setAnchorEl(event.currentTarget)}
sx={buttonStyle}
Expand All @@ -95,7 +95,7 @@ export const NavigationButton = ({ item }: { item: NavigationItem }) => {
showItem(subItem.key) && (
<MenuItem
component={Link}
data-cy={`navbar-${subItem.key}`}
data-cy={`nav-bar-button-${subItem.key}`}
key={subItem.path}
onClick={() => setAnchorEl(null)}
selected={location.pathname.includes(subItem.path)}
Expand All @@ -106,12 +106,12 @@ export const NavigationButton = ({ item }: { item: NavigationItem }) => {
)
)}
</Menu>
</Fragment>
</>
)
}

return (
<Button component={Link} data-cy={`navbar-${key}`} sx={buttonStyle} to={path}>
<Button component={Link} data-cy={`nav-bar-button-${key}`} sx={buttonStyle} to={path}>
{label}
</Button>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ export const UserButton = () => {

return (
<Box sx={{ flexGrow: 0 }}>
<IconButton color="inherit" onClick={event => setAnchorEl(event.currentTarget)} sx={{ p: 0 }}>
<IconButton
color="inherit"
data-cy="nav-bar-user-button"
onClick={event => setAnchorEl(event.currentTarget)}
sx={{ p: 0 }}
>
<AccountCircle />
</IconButton>
<Menu
Expand All @@ -45,7 +50,7 @@ export const UserButton = () => {
{!isLoading && username && (
<MenuItem sx={{ pointerEvents: 'none' }}>
<Typography>
{mockedBy ? 'Mocked' : 'Logged in'} as <b>{username}</b>
{mockedBy ? 'Mocking' : 'Logged in'} as <b>{username}</b>
</Typography>
</MenuItem>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const NavigationBar = () => {
<Logo />
{!isLoading && (
<>
<Box sx={{ display: 'flex', flexGrow: 1, justifyContent: 'space-between' }}>
<Box data-cy="nav-bar" sx={{ display: 'flex', flexGrow: 1, justifyContent: 'space-between' }}>
<NavigationDivider />
{Object.values(visibleNavigationItems).map(item => (
<Fragment key={item.key}>
Expand Down

0 comments on commit 3c09f21

Please sign in to comment.