2015-12-07 00:24:06 +03:00
/* jshint expr:true */
import {
describe ,
it ,
beforeEach ,
afterEach
} from 'mocha' ;
import { expect } from 'chai' ;
import Ember from 'ember' ;
import startApp from '../helpers/start-app' ;
import destroyApp from '../helpers/destroy-app' ;
2016-05-24 15:06:59 +03:00
import { invalidateSession , authenticateSession } from 'ghost-admin/tests/helpers/ember-simple-auth' ;
import { errorOverride , errorReset } from 'ghost-admin/tests/helpers/adapter-error' ;
2015-12-07 00:24:06 +03:00
import Mirage from 'ember-cli-mirage' ;
describe ( 'Acceptance: Team' , function ( ) {
let application ;
beforeEach ( function ( ) {
application = startApp ( ) ;
} ) ;
afterEach ( function ( ) {
destroyApp ( application ) ;
} ) ;
it ( 'redirects to signin when not authenticated' , function ( ) {
invalidateSession ( application ) ;
visit ( '/team' ) ;
andThen ( function ( ) {
expect ( currentURL ( ) ) . to . equal ( '/signin' ) ;
} ) ;
} ) ;
it ( 'redirects correctly when authenticated as author' , function ( ) {
2016-04-08 18:00:16 +03:00
let role = server . create ( 'role' , { name : 'Author' } ) ;
let user = server . create ( 'user' , { roles : [ role ] , slug : 'test-user' } ) ;
2015-12-07 00:24:06 +03:00
server . create ( 'user' , { slug : 'no-access' } ) ;
authenticateSession ( application ) ;
visit ( '/team/no-access' ) ;
andThen ( ( ) => {
expect ( currentURL ( ) , 'currentURL' ) . to . equal ( '/team/test-user' ) ;
} ) ;
} ) ;
it ( 'redirects correctly when authenticated as editor' , function ( ) {
2016-04-08 18:00:16 +03:00
let role = server . create ( 'role' , { name : 'Editor' } ) ;
let user = server . create ( 'user' , { roles : [ role ] , slug : 'test-user' } ) ;
2015-12-07 00:24:06 +03:00
server . create ( 'user' , { slug : 'no-access' } ) ;
authenticateSession ( application ) ;
visit ( '/team/no-access' ) ;
andThen ( ( ) => {
expect ( currentURL ( ) , 'currentURL' ) . to . equal ( '/team' ) ;
} ) ;
} ) ;
describe ( 'when logged in' , function ( ) {
beforeEach ( function ( ) {
2016-04-08 18:00:16 +03:00
let role = server . create ( 'role' , { name : 'Admininstrator' } ) ;
let user = server . create ( 'user' , { roles : [ role ] } ) ;
2015-12-07 00:24:06 +03:00
server . loadFixtures ( ) ;
return authenticateSession ( application ) ;
} ) ;
it ( 'it renders and navigates correctly' , function ( ) {
2016-04-08 18:00:16 +03:00
let user1 = server . create ( 'user' ) ;
let user2 = server . create ( 'user' ) ;
2015-12-07 00:24:06 +03:00
visit ( '/team' ) ;
andThen ( ( ) => {
// doesn't do any redirecting
expect ( currentURL ( ) , 'currentURL' ) . to . equal ( '/team' ) ;
// it has correct page title
expect ( document . title , 'page title' ) . to . equal ( 'Team - Test Blog' ) ;
// it shows 3 users in list (includes currently logged in user)
expect ( find ( '.user-list .user-list-item' ) . length , 'user list count' )
. to . equal ( 3 ) ;
click ( '.user-list-item:last' ) ;
andThen ( ( ) => {
// url is correct
expect ( currentURL ( ) , 'url after clicking user' ) . to . equal ( ` /team/ ${ user2 . slug } ` ) ;
// title is correct
expect ( document . title , 'title after clicking user' ) . to . equal ( 'Team - User - Test Blog' ) ;
// view title should exist and be linkable and active
expect ( find ( '.view-title a[href="/team"]' ) . hasClass ( 'active' ) , 'has linkable url back to team main page' )
. to . be . true ;
} ) ;
click ( '.view-title a' ) ;
andThen ( ( ) => {
// url should be /team again
expect ( currentURL ( ) , 'url after clicking back' ) . to . equal ( '/team' ) ;
} ) ;
} ) ;
} ) ;
describe ( 'invite new user' , function ( ) {
2015-11-18 13:50:48 +03:00
let emailInputField = '.fullscreen-modal input[name="email"]' ;
2015-12-07 00:24:06 +03:00
// @TODO: Evaluate after the modal PR goes in
it ( 'modal loads correctly' , function ( ) {
visit ( '/team' ) ;
andThen ( ( ) => {
// url is correct
expect ( currentURL ( ) , 'currentURL' ) . to . equal ( '/team' ) ;
// invite user button exists
expect ( find ( '.view-actions .btn-green' ) . html ( ) , 'invite people button text' )
. to . equal ( 'Invite People' ) ;
} ) ;
click ( '.view-actions .btn-green' ) ;
andThen ( ( ) => {
let roleOptions = find ( '#new-user-role select option' ) ;
function checkOwnerExists ( ) {
for ( let i in roleOptions ) {
if ( roleOptions [ i ] . tagName === 'option' && roleOptions [ i ] . text === 'Owner' ) {
return true ;
}
}
return false ;
}
function checkSelectedIsAuthor ( ) {
for ( let i in roleOptions ) {
if ( roleOptions [ i ] . selected ) {
return roleOptions [ i ] . text === 'Author' ;
}
}
return false ;
}
// should be 3 available roles
expect ( roleOptions . length , 'number of available roles' ) . to . equal ( 3 ) ;
expect ( checkOwnerExists ( ) , 'owner role isn\'t available' ) . to . be . false ;
expect ( checkSelectedIsAuthor ( ) , 'author role is selected initially' ) . to . be . true ;
} ) ;
} ) ;
it ( 'sends an invite correctly' , function ( ) {
visit ( '/team' ) ;
andThen ( ( ) => {
expect ( find ( '.user-list.invited-users .user-list-item' ) . length , 'number of invited users' ) . to . equal ( 0 ) ;
} ) ;
click ( '.view-actions .btn-green' ) ;
click ( emailInputField ) ;
triggerEvent ( emailInputField , 'blur' ) ;
andThen ( ( ) => {
expect ( find ( '.modal-body .form-group:first' ) . hasClass ( 'error' ) , 'email input has error status' ) . to . be . true ;
expect ( find ( '.modal-body .form-group:first .response' ) . text ( ) ) . to . contain ( 'Please enter an email.' ) ;
} ) ;
fillIn ( emailInputField , 'test@example.com' ) ;
2015-11-18 13:50:48 +03:00
click ( '.fullscreen-modal .btn-green' ) ;
2015-12-07 00:24:06 +03:00
andThen ( ( ) => {
expect ( find ( '.user-list.invited-users .user-list-item' ) . length , 'number of invited users' ) . to . equal ( 1 ) ;
expect ( find ( '.user-list.invited-users .user-list-item:first .name' ) . text ( ) , 'name of invited user' ) . to . equal ( 'test@example.com' ) ;
} ) ;
click ( '.user-list.invited-users .user-list-item:first .user-list-item-aside .user-list-action:contains("Revoke")' ) ;
andThen ( ( ) => {
expect ( find ( '.user-list.invited-users .user-list-item' ) . length , 'number of invited users' ) . to . equal ( 0 ) ;
} ) ;
} ) ;
it ( 'fails sending an invite correctly' , function ( ) {
server . create ( 'user' , { email : 'test1@example.com' } ) ;
server . create ( 'user' , { email : 'test2@example.com' , status : 'invited' } ) ;
visit ( '/team' ) ;
2015-11-18 13:50:48 +03:00
// check our users lists are what we expect
2015-12-07 00:24:06 +03:00
andThen ( ( ) => {
2015-11-18 13:50:48 +03:00
expect ( find ( '.user-list.invited-users .user-list-item' ) . length , 'number of invited users' )
. to . equal ( 1 ) ;
2015-12-07 00:24:06 +03:00
// number of active users is 2 because of the logged-in user
2015-11-18 13:50:48 +03:00
expect ( find ( '.user-list.active-users .user-list-item' ) . length , 'number of active users' )
. to . equal ( 2 ) ;
2015-12-07 00:24:06 +03:00
} ) ;
2015-11-18 13:50:48 +03:00
// click the "invite new user" button to open the modal
2015-12-07 00:24:06 +03:00
click ( '.view-actions .btn-green' ) ;
2015-11-18 13:50:48 +03:00
// fill in and submit the invite user modal with an existing user
2015-12-07 00:24:06 +03:00
fillIn ( emailInputField , 'test1@example.com' ) ;
2015-11-18 13:50:48 +03:00
click ( '.fullscreen-modal .btn-green' ) ;
2015-12-07 00:24:06 +03:00
andThen ( ( ) => {
2015-11-18 13:50:48 +03:00
// check the inline-validation
expect ( find ( '.fullscreen-modal .error .response' ) . text ( ) . trim ( ) , 'inviting existing user error' )
. to . equal ( 'A user with that email address already exists.' ) ;
2015-12-07 00:24:06 +03:00
} ) ;
2015-11-18 13:50:48 +03:00
// fill in and submit the invite user modal with an invited user
2015-12-07 00:24:06 +03:00
fillIn ( emailInputField , 'test2@example.com' ) ;
2015-11-18 13:50:48 +03:00
click ( '.fullscreen-modal .btn-green' ) ;
2015-12-07 00:24:06 +03:00
andThen ( ( ) => {
2015-11-18 13:50:48 +03:00
// check the inline-validation
expect ( find ( '.fullscreen-modal .error .response' ) . text ( ) . trim ( ) , 'inviting invited user error' )
. to . equal ( 'A user with that email address was already invited.' ) ;
// ensure that there's been no change in our user lists
expect ( find ( '.user-list.invited-users .user-list-item' ) . length , 'number of invited users after failed invites' )
. to . equal ( 1 ) ;
expect ( find ( '.user-list.active-users .user-list-item' ) . length , 'number of active users after failed invites' )
. to . equal ( 2 ) ;
2015-12-07 00:24:06 +03:00
} ) ;
} ) ;
} ) ;
describe ( 'existing user' , function ( ) {
let user ;
beforeEach ( function ( ) {
2016-05-17 21:14:14 +03:00
server . create ( 'user' , {
slug : 'test-1' ,
name : 'Test User' ,
facebook : 'test' ,
twitter : '@test'
} ) ;
2016-03-03 11:52:27 +03:00
server . loadFixtures ( ) ;
2015-12-07 00:24:06 +03:00
} ) ;
it ( 'input fields reset and validate correctly' , function ( ) {
// test user name
visit ( '/team/test-1' ) ;
andThen ( ( ) => {
expect ( currentURL ( ) , 'currentURL' ) . to . equal ( '/team/test-1' ) ;
expect ( find ( '.user-details-top .first-form-group input.user-name' ) . val ( ) , 'current user name' ) . to . equal ( 'Test User' ) ;
} ) ;
// test empty user name
fillIn ( '.user-details-top .first-form-group input.user-name' , '' ) ;
triggerEvent ( '.user-details-top .first-form-group input.user-name' , 'blur' ) ;
andThen ( ( ) => {
2016-04-25 12:54:36 +03:00
expect ( find ( '.user-details-top .first-form-group' ) . hasClass ( 'error' ) , 'username input is in error state with blank input' ) . to . be . true ;
2015-12-07 00:24:06 +03:00
} ) ;
// test too long user name
fillIn ( '.user-details-top .first-form-group input.user-name' , new Array ( 160 ) . join ( 'a' ) ) ;
triggerEvent ( '.user-details-top .first-form-group input.user-name' , 'blur' ) ;
andThen ( ( ) => {
2016-04-25 12:54:36 +03:00
expect ( find ( '.user-details-top .first-form-group' ) . hasClass ( 'error' ) , 'username input is in error state with too long input' ) . to . be . true ;
2015-12-07 00:24:06 +03:00
} ) ;
// reset name field
fillIn ( '.user-details-top .first-form-group input.user-name' , 'Test User' ) ;
andThen ( ( ) => {
expect ( find ( '.user-details-bottom input[name="user"]' ) . val ( ) , 'slug value is default' ) . to . equal ( 'test-1' ) ;
} ) ;
fillIn ( '.user-details-bottom input[name="user"]' , '' ) ;
triggerEvent ( '.user-details-bottom input[name="user"]' , 'blur' ) ;
andThen ( ( ) => {
expect ( find ( '.user-details-bottom input[name="user"]' ) . val ( ) , 'slug value is reset to original upon empty string' ) . to . equal ( 'test-1' ) ;
} ) ;
fillIn ( '.user-details-bottom input[name="user"]' , 'white space' ) ;
triggerEvent ( '.user-details-bottom input[name="user"]' , 'blur' ) ;
andThen ( ( ) => {
expect ( find ( '.user-details-bottom input[name="user"]' ) . val ( ) , 'slug value is correctly dasherized' ) . to . equal ( 'white-space' ) ;
} ) ;
fillIn ( '.user-details-bottom input[name="email"]' , 'thisisnotanemail' ) ;
triggerEvent ( '.user-details-bottom input[name="email"]' , 'blur' ) ;
andThen ( ( ) => {
2016-04-25 12:54:36 +03:00
expect ( find ( '.user-details-bottom .form-group:nth-of-type(2)' ) . hasClass ( 'error' ) , 'email input should be in error state with invalid email' ) . to . be . true ;
2015-12-07 00:24:06 +03:00
} ) ;
fillIn ( '.user-details-bottom input[name="email"]' , 'test@example.com' ) ;
fillIn ( '#user-location' , new Array ( 160 ) . join ( 'a' ) ) ;
triggerEvent ( '#user-location' , 'blur' ) ;
andThen ( ( ) => {
expect ( find ( '.user-details-bottom .form-group:nth-of-type(3)' ) . hasClass ( 'error' ) , 'location input should be in error state' ) . to . be . true ;
} ) ;
fillIn ( '#user-location' , '' ) ;
fillIn ( '#user-website' , 'thisisntawebsite' ) ;
triggerEvent ( '#user-website' , 'blur' ) ;
andThen ( ( ) => {
expect ( find ( '.user-details-bottom .form-group:nth-of-type(4)' ) . hasClass ( 'error' ) , 'website input should be in error state' ) . to . be . true ;
} ) ;
2016-05-16 21:16:40 +03:00
// Testing Facebook input
2016-05-17 21:14:14 +03:00
andThen ( ( ) => {
// displays initial value
expect ( find ( '#user-facebook' ) . val ( ) , 'initial facebook value' )
. to . equal ( 'https://www.facebook.com/test' ) ;
} ) ;
triggerEvent ( '#user-facebook' , 'focus' ) ;
triggerEvent ( '#user-facebook' , 'blur' ) ;
andThen ( ( ) => {
// regression test: we still have a value after the input is
// focused and then blurred without any changes
expect ( find ( '#user-facebook' ) . val ( ) , 'facebook value after blur with no change' )
. to . equal ( 'https://www.facebook.com/test' ) ;
} ) ;
2016-03-03 11:52:27 +03:00
fillIn ( '#user-facebook' , '' ) ;
fillIn ( '#user-facebook' , ')(*&%^%)' ) ;
triggerEvent ( '#user-facebook' , 'blur' ) ;
andThen ( ( ) => {
expect ( find ( '.user-details-bottom .form-group:nth-of-type(5)' ) . hasClass ( 'error' ) , 'facebook input should be in error state' ) . to . be . true ;
} ) ;
fillIn ( '#user-facebook' , '' ) ;
2016-05-16 21:16:40 +03:00
fillIn ( '#user-facebook' , 'pages/)(*&%^%)' ) ;
2016-03-03 11:52:27 +03:00
triggerEvent ( '#user-facebook' , 'blur' ) ;
andThen ( ( ) => {
2016-05-16 21:16:40 +03:00
expect ( find ( '#user-facebook' ) . val ( ) ) . to . be . equal ( 'https://www.facebook.com/pages/)(*&%^%)' ) ;
2016-03-03 11:52:27 +03:00
expect ( find ( '.user-details-bottom .form-group:nth-of-type(5)' ) . hasClass ( 'error' ) , 'facebook input should be in error state' ) . to . be . false ;
} ) ;
fillIn ( '#user-facebook' , '' ) ;
2016-05-16 21:16:40 +03:00
fillIn ( '#user-facebook' , 'testing' ) ;
triggerEvent ( '#user-facebook' , 'blur' ) ;
andThen ( ( ) => {
expect ( find ( '#user-facebook' ) . val ( ) ) . to . be . equal ( 'https://www.facebook.com/testing' ) ;
expect ( find ( '.user-details-bottom .form-group:nth-of-type(5)' ) . hasClass ( 'error' ) , 'facebook input should be in error state' ) . to . be . false ;
} ) ;
fillIn ( '#user-facebook' , '' ) ;
fillIn ( '#user-facebook' , 'somewebsite.com/pages/some-facebook-page/857469375913?ref=ts' ) ;
triggerEvent ( '#user-facebook' , 'blur' ) ;
andThen ( ( ) => {
expect ( find ( '#user-facebook' ) . val ( ) ) . to . be . equal ( 'https://www.facebook.com/pages/some-facebook-page/857469375913?ref=ts' ) ;
expect ( find ( '.user-details-bottom .form-group:nth-of-type(5)' ) . hasClass ( 'error' ) , 'facebook input should be in error state' ) . to . be . false ;
} ) ;
fillIn ( '#user-facebook' , '' ) ;
fillIn ( '#user-facebook' , 'test' ) ;
2016-03-03 11:52:27 +03:00
triggerEvent ( '#user-facebook' , 'blur' ) ;
andThen ( ( ) => {
expect ( find ( '.user-details-bottom .form-group:nth-of-type(5)' ) . hasClass ( 'error' ) , 'facebook input should be in error state' ) . to . be . true ;
} ) ;
fillIn ( '#user-facebook' , '' ) ;
2016-05-16 21:16:40 +03:00
fillIn ( '#user-facebook' , 'http://twitter.com/testuser' ) ;
triggerEvent ( '#user-facebook' , 'blur' ) ;
andThen ( ( ) => {
expect ( find ( '#user-facebook' ) . val ( ) ) . to . be . equal ( 'https://www.facebook.com/testuser' ) ;
expect ( find ( '.user-details-bottom .form-group:nth-of-type(5)' ) . hasClass ( 'error' ) , 'facebook input should be in error state' ) . to . be . false ;
} ) ;
fillIn ( '#user-facebook' , '' ) ;
fillIn ( '#user-facebook' , 'facebook.com/testing' ) ;
2016-03-03 11:52:27 +03:00
triggerEvent ( '#user-facebook' , 'blur' ) ;
andThen ( ( ) => {
2016-05-16 21:16:40 +03:00
expect ( find ( '#user-facebook' ) . val ( ) ) . to . be . equal ( 'https://www.facebook.com/testing' ) ;
2016-03-03 11:52:27 +03:00
expect ( find ( '.user-details-bottom .form-group:nth-of-type(5)' ) . hasClass ( 'error' ) , 'facebook input should be in error state' ) . to . be . false ;
} ) ;
2016-05-16 21:16:40 +03:00
// Testing Twitter input
2016-05-17 21:14:14 +03:00
andThen ( ( ) => {
// loads fixtures and performs transform
expect ( find ( '#user-twitter' ) . val ( ) , 'initial twitter value' )
. to . equal ( 'https://twitter.com/test' ) ;
} ) ;
triggerEvent ( '#user-twitter' , 'focus' ) ;
triggerEvent ( '#user-twitter' , 'blur' ) ;
andThen ( ( ) => {
// regression test: we still have a value after the input is
// focused and then blurred without any changes
expect ( find ( '#user-twitter' ) . val ( ) , 'twitter value after blur with no change' )
. to . equal ( 'https://twitter.com/test' ) ;
} ) ;
2016-03-03 11:52:27 +03:00
fillIn ( '#user-twitter' , '' ) ;
fillIn ( '#user-twitter' , ')(*&%^%)' ) ;
triggerEvent ( '#user-twitter' , 'blur' ) ;
andThen ( ( ) => {
expect ( find ( '.user-details-bottom .form-group:nth-of-type(6)' ) . hasClass ( 'error' ) , 'twitter input should be in error state' ) . to . be . true ;
} ) ;
fillIn ( '#user-twitter' , '' ) ;
fillIn ( '#user-twitter' , 'name' ) ;
triggerEvent ( '#user-twitter' , 'blur' ) ;
andThen ( ( ) => {
expect ( find ( '#user-twitter' ) . val ( ) ) . to . be . equal ( 'https://twitter.com/name' ) ;
expect ( find ( '.user-details-bottom .form-group:nth-of-type(6)' ) . hasClass ( 'error' ) , 'twitter input should be in error state' ) . to . be . false ;
} ) ;
fillIn ( '#user-twitter' , '' ) ;
fillIn ( '#user-twitter' , 'http://github.com/user' ) ;
triggerEvent ( '#user-twitter' , 'blur' ) ;
andThen ( ( ) => {
2016-05-16 21:16:40 +03:00
expect ( find ( '#user-twitter' ) . val ( ) ) . to . be . equal ( 'https://twitter.com/user' ) ;
expect ( find ( '.user-details-bottom .form-group:nth-of-type(6)' ) . hasClass ( 'error' ) , 'twitter input should be in error state' ) . to . be . false ;
2016-03-03 11:52:27 +03:00
} ) ;
fillIn ( '#user-twitter' , '' ) ;
fillIn ( '#user-twitter' , 'twitter.com/user' ) ;
triggerEvent ( '#user-twitter' , 'blur' ) ;
andThen ( ( ) => {
expect ( find ( '#user-twitter' ) . val ( ) ) . to . be . equal ( 'https://twitter.com/user' ) ;
expect ( find ( '.user-details-bottom .form-group:nth-of-type(6)' ) . hasClass ( 'error' ) , 'twitter input should be in error state' ) . to . be . false ;
} ) ;
2015-12-07 00:24:06 +03:00
fillIn ( '#user-website' , '' ) ;
fillIn ( '#user-bio' , new Array ( 210 ) . join ( 'a' ) ) ;
triggerEvent ( '#user-bio' , 'blur' ) ;
andThen ( ( ) => {
2016-03-03 11:52:27 +03:00
expect ( find ( '.user-details-bottom .form-group:nth-of-type(7)' ) . hasClass ( 'error' ) , 'bio input should be in error state' ) . to . be . true ;
2015-12-07 00:24:06 +03:00
} ) ;
} ) ;
} ) ;
2016-02-05 15:16:29 +03:00
it ( 'redirects to 404 when tag does not exist' , function ( ) {
server . get ( '/users/slug/unknown/' , function ( ) {
return new Mirage . Response ( 404 , { 'Content-Type' : 'application/json' } , { errors : [ { message : 'User not found.' , errorType : 'NotFoundError' } ] } ) ;
2015-12-07 00:24:06 +03:00
} ) ;
2016-02-05 15:16:29 +03:00
errorOverride ( ) ;
2015-12-07 00:24:06 +03:00
2016-02-05 15:16:29 +03:00
visit ( '/team/unknown' ) ;
2015-12-07 00:24:06 +03:00
2016-02-05 15:16:29 +03:00
andThen ( ( ) => {
errorReset ( ) ;
expect ( currentPath ( ) ) . to . equal ( 'error404' ) ;
expect ( currentURL ( ) ) . to . equal ( '/team/unknown' ) ;
2015-12-07 00:24:06 +03:00
} ) ;
} ) ;
} ) ;
} ) ;