public static LabResponse GetLabUserData(UserQueryParameters query)
        {
            var user = _labService.GetLabResponse(query);

            if (user == null)
            {
                throw new LabUserNotFoundException(query, "Found no users for the given query.");
            }
            return(user);
        }
        /// <summary>
        /// Returns a test user account for use in testing.
        /// </summary>
        /// <param name="query">Any and all parameters that the returned user should satisfy.</param>
        /// <returns>Users that match the given query parameters.</returns>
        public LabResponse GetLabResponse(UserQueryParameters query)
        {
            var response = GetLabResponseFromAPI(query);
            var user     = response.User;

            if (!Uri.IsWellFormedUriString(user.CredentialUrl, UriKind.Absolute))
            {
                Console.WriteLine($"User '{user.Upn}' has invalid Credential URL: '{user.CredentialUrl}'");
            }

            if (user.IsExternal && user.HomeUser == null)
            {
                Console.WriteLine($"User '{user.Upn}' has no matching home user.");
            }

            return(response);
        }
        private LabResponse GetLabResponseFromAPI(UserQueryParameters query)
        {
            HttpClient webClient = new HttpClient();
            IDictionary <string, string> queryDict = new Dictionary <string, string>
            {
                // Disabled for now until there are tests that use it.
                { "mamca", "false" },
                { "mdmca", "false" }
            };

            //Building user query
            if (query.FederationProvider != null)
            {
                queryDict.Add("federationProvider", query.FederationProvider.ToString());
            }

            queryDict.Add("mam", query.IsMamUser.HasValue && query.IsMamUser.Value ? "true" : "false");
            queryDict.Add("mfa", query.IsMfaUser.HasValue && query.IsMfaUser.Value ? "true" : "false");

            if (query.Licenses != null && query.Licenses.Count > 0)
            {
                queryDict.Add("license", query.Licenses.ToArray().ToString());
            }

            queryDict.Add("isFederated", query.IsFederatedUser.HasValue && query.IsFederatedUser.Value ? "true" : "false");

            if (query.UserType != null)
            {
                queryDict.Add("usertype", query.UserType.ToString());
            }

            queryDict.Add("external", query.IsExternalUser.HasValue && query.IsExternalUser.Value ? "true" : "false");

            if (query.B2CIdentityProvider == B2CIdentityProvider.Local)
            {
                queryDict.Add("b2cProvider", "local");
            }

            if (query.B2CIdentityProvider == B2CIdentityProvider.Facebook)
            {
                queryDict.Add("b2cProvider", "facebook");
            }

            if (query.B2CIdentityProvider == B2CIdentityProvider.Google)
            {
                queryDict.Add("b2cProvider", "google");
            }

            UriBuilder uriBuilder = new UriBuilder("http://api.msidlab.com/api/user")
            {
                Query = string.Join("&", queryDict.Select(x => x.Key + "=" + x.Value.ToString()))
            };

            //Fetch user
            string result = webClient.GetStringAsync(uriBuilder.ToString()).GetAwaiter().GetResult();

            if (string.IsNullOrWhiteSpace(result))
            {
                throw new LabUserNotFoundException(query, "No lab user with specified parameters exists");
            }

            LabUser user = JsonConvert.DeserializeObject <LabUser>(result);

            if (!string.IsNullOrEmpty(user.HomeTenantId) && !string.IsNullOrEmpty(user.HomeUPN))
            {
                user.InitializeHomeUser();
            }

            return(JsonConvert.DeserializeObject <LabResponse>(result));;
        }
 public LabUserNotFoundException(UserQueryParameters parameters, string message) : base(message)
 {
     Parameters = parameters;
 }