Utility.Tests.ps1

using module .\Utility.psm1

Describe "IsValidEmail" -Tags "Unit" {
  It "return true for valid email" {
    IsValidEmail -In "user@contoso.com" | Should be $true
  }
  It "return false for invalid email" {
    IsValidEmail -In "user@d@contoso.com" | Should be $false
  }
  It "return false for empty input" {
    IsValidEmail -In "" | Should be $false
  }
}

Describe "ParseAccessToken" -Tags "Unit" {
  It "can parse user accessToken" {
    $accessToken = "eyJ0eXAiOiJKV1QiLCJub25jZSI6IjlJcFh2d3c2LTB1a0I5M3VXWjlfSUwtTC1ZdU5jNGY0LWRYTFVRWVZvRVUiLCJhbGciOiJSUzI1NiIsIng1dCI6Ii1LSTNROW5OUjdiUm9meG1lWm9YcWJIWkdldyIsImtpZCI6Ii1LSTNROW5OUjdiUm9meG1lWm9YcWJIWkdldyJ9.eyJhdWQiOiJodHRwczovL291dGxvb2sub2ZmaWNlMzY1LmNvbSIsImlzcyI6Imh0dHBzOi8vc3RzLndpbmRvd3MubmV0L2E0NDVmYjA4LTUwODQtNGYxZi1hNjQ1LTZiZGUxNTNlZDEzNy8iLCJpYXQiOjE2ODExNzg1NjIsIm5iZiI6MTY4MTE3ODU2MiwiZXhwIjoxNjgxMTg0MTY4LCJhY2N0IjowLCJhY3IiOiIxIiwiYWlvIjoiQVRRQXkvOFRBQUFBTGw1MDQ0Q1F2d1FqOE9oTk00MzQ3YUtUZ3JUUkJRY0REbDl5RlJZMHFxZ2NlSDlXejd1R3ZOcDBmdytDcFRudiIsImFtciI6WyJwd2QiXSwiYXBwX2Rpc3BsYXluYW1lIjoiUG9wSW1hcCIsImFwcGlkIjoiMjEzZGMwYzItYTNkNi00MDE1LTljMGUtNzZlZWM2M2VkYTM4IiwiYXBwaWRhY3IiOiIwIiwiZW5mcG9saWRzIjpbXSwiZmFtaWx5X25hbWUiOiJWYW5jZSIsImdpdmVuX25hbWUiOiJBZGVsZSIsImlwYWRkciI6IjE2Ny4yMjAuMjU1LjEwIiwibmFtZSI6IkFkZWxlIFZhbmNlIiwib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Object.scp | Should be "IMAP.AccessAsUser.All User.Read"
    $tokenObject.aud | Should be "https://outlook.office365.com"
  }
  It "can parse app accessToken" {
    $accessToken = "eyJ0eXAiOiJKV1QiLCJub25jZSI6Ikg3TUlZV0hPc3gxOVB3UmpGdjRWUGtyY2gtMWZhQVZDaTBTUExHZDdncmMiLCJhbGciOiJSUzI1NiIsIng1dCI6Ii1LSTNROW5OUjdiUm9meG1lWm9YcWJIWkdldyIsImtpZCI6Ii1LSTNROW5OUjdiUm9meG1lWm9YcWJIWkdldyJ9.eyJhdWQiOiJodHRwczovL291dGxvb2sub2ZmaWNlMzY1LmNvbSIsImlzcyI6Imh0dHBzOi8vc3RzLndpbmRvd3MubmV0L2E0NDVmYjA4LTUwODQtNGYxZi1hNjQ1LTZiZGUxNTNlZDEzNy8iLCJpYXQiOjE2ODExNDIzNzQsIm5iZiI6MTY4MTE0MjM3NCwiZXhwIjoxNjgxMTQ2Mjc0LCJhaW8iOiJFMlpnWUZoeW42SEEvTkxQT05XUElVdW1XUXFsQXdBPSIsImFwcF9kaXNwbGF5bmFtZSI6IlBvcEltYXAiLCJhcHBpZCI6IjIxM2RjMGMyLWEzZDYtNDAxNS05YzBlLTc2ZWVjNjNlZGEzOCIsImFwcGlkYWNyIjoiMSIsImlkcCI6Imh0dHBzOi8vc3RzLndpbmRvd3MubmV0L2E0NDVmYjA4LTUwODQtNGYxZi1hNjQ1LTZiZGUxNTNlZDEzNy8iLCJvaWQiOiI3OTY1NTIwYy01YmRmLTQ3MDAtODVjNi0xMDZjYThmOWZmMzAiLCJyaCI6IjAuQVZZQUNQdEZwSVJRSDAtbVJXdmVGVDdSTndJQUFBQUFBUEVQemdBQUFBQUFBQUNmQUFBLiIsInJvbGVzIjpbIlBPUC5BY2Nlc3NBc0FwcCIsIklNQVAuQWNjZXNzQXNBcHAiXSwic2lkIjoiNjFiY2QzZTEtM2FmMS00MGQyLWJhMTQtNTVjZmFhYjA5ZjMzIiwic3ViIjoiNzk2NTUyMGMtNWJkZi00NzAwLTg1YzYtMTA2Y2E4ZjlmZjMwIiwidGlkIjoiYTQ0NWZiMDgtNTA4NC00ZjFmLWE2NDUtNmJkZTE1M2VkMTM3IiwidXRpIjoidWhBeG42ZHRORUtoNF9pUWowZ0RBUSIsInZlciI6IjEuMCIsIndpZHMiOlsiMDk5N2ExZDAtMGQxZC00YWNiLWI0MDgtZDVjYTczMTIxZTkwIl19.YSVp914tiLU80AYZm_mIQZnwPX7bVrcyaTYVoljroMTVcix70c5ULsJFJoX0dX8pDTqXNs2HUkxIeSZLnLmZXEedefODTl944FLN0EoXCHEbaPsM_XcVez-3iMTLsDpVkdvVMmiI9d1i1TKEySbCilET9-UAgXSqGW1WWBmmEarfai5p_YDQUDWK97grewRnzYiSSHc8W_6axyFJw4dIyryfB6yoGRc8I-Vnnk1QNOY7AvieQll5aR0XRiOSpiqB2e5cn-rSXyLRIZS3z2g3foBQ86yA926wqXdfQ-QFCCzZvTMDdDxUJ27UagEer2tspijrzAFZktpmQ4QoowBI5Q"
    $tokenObject = ParseAccessToken -AccessToken $accessToken
    $tokenObject.roles.Contains("IMAP.AccessAsApp") | Should be $true
    
  }
}

Describe "BuildO365Token" -Tags "Unit" {
  It "combines upn and access token" {
    $accessToken = "eyJ0eXAiOiJKV1QiLCJub25jZSI6ImR5ZGtITXZuUHZrc1ROZ3Z3QWdvVk1aaFdoekMtTVJNeE9HMDctVmlIcUUiLCJhbGciOiJSUzI1NiIsIng1dCI6Ii1LSTNROW5OUjdiUm9meG1lWm9YcWJIWkdldyIsImtpZCI6Ii1LSTNROW5OUjdiUm9meG1lWm9YcWJIWkdldyJ9.eyJhdWQiOiJodHRwczovL291dGxvb2sub2ZmaWNlLmNvbSIsImlzcyI6Imh0dHBzOi8vc3RzLndpbmRvd3MubmV0L2E0NDVmYjA4LTUwODQtNGYxZi1hNjQ1LTZiZGUxNTNlZDEzNy8iLCJpYXQiOjE2NzkxNDQ5OTEsIm5iZiI6MTY3OTE0NDk5MSwiZXhw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GttVl94aXFRdjA5QUEiLCJ2ZXIiOiIxLjAiLCJ3aWRzIjpbIjYyZTkwMzk0LTY5ZjUtNDIzNy05MTkwLTAxMjE3NzE0NWUxMCIsImI3OWZiZjRkLTNlZjktNDY4OS04MTQzLTc2YjE5NGU4NTUwOSJdfQ.PCLnllA4vVrKH21gD3MVWw6oAK8uBoMSmaJwCs3nDGQwPh6Yy_NXBn6CHutDm4ebDruT46WoFoFoO2oUaRbrJu7rJxNPpZMFHly8fkn9sYQPbd6AAX8lCk38nwDRl8b7QlSvtWI0HYlSoV329R1-ymS1KvuujjnvNPgQR71onvp4DU4pTcPzIU0uo937LY9S5UyjhpudtnVAYi9F6dcMuzaX4dHctCEgr9U9jZ2Hfv631sBPZb30uEIuJB76QgC2DMjHsUQgiflvkwlIz0YPX_CTtUZuQZqjiiC2RZDT1luCh1PRVwZKJGDTqnhDFQiMQF8fFsmyR1qZpqz1GDj-NA"
    $upn = "admin@M365x43963602.onmicrosoft.com"
    $token = BuildO365Token -AccessToken $accessToken -Upn $upn
    $token | Should be "dXNlcj1hZG1pbkBNMzY1eDQzOTYzNjAyLm9ubWljcm9zb2Z0LmNvbQFhdXRoPUJlYXJlciBleUowZVhBaU9pSktWMVFpTENKdWIyNWpaU0k2SW1SNVpHdElUWFp1VUhacmMxUk9aM1ozUVdkdlZrMWFhRmRvZWtNdFRWSk5lRTlITURjdFZtbEljVVVpTENKaGJHY2lPaUpTVXpJMU5pSXNJbmcxZENJNklpMUxTVE5ST1c1T1VqZGlVbTltZUcxbFdtOVljV0pJV2tkbGR5SXNJbXRwWkNJNklpMUxTVE5ST1c1T1VqZGlVbTltZUcxbFdtOVljV0pJV2tkbGR5SjkuZXlKaGRXUWlPaUpvZEhSd2N6b3ZMMjkxZEd4dmIyc3ViMlptYVdObExtTnZiU0lzSW1semN5STZJbWgwZEhCek9pOHZjM1J6TG5kcGJtUnZkM011Ym1WMEwyRTBORFZtWWpBNExUVXdPRFF0TkdZeFppMWhOalExTFRaaVp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