public async Task ReturnsUnauthorizedResultIfNotAuthenticated() { // Arrange var request = new DefaultHttpContext().Request; string xsrfToken = $"xsrf-token-{DateTime.Now.Ticks}"; request.Headers.Add("Cookie", new CookieHeaderValue(Globals.XsrfTokenCookieAndHeaderName, xsrfToken).ToString()); request.Headers.Add(Globals.XsrfTokenCookieAndHeaderName, xsrfToken); var durableClientMoq = new Mock <IDurableClient>(); var logMoq = new Mock <ILogger>(); // Getting the list of all functions to be validated var functionsToBeCalled = typeof(DfmEndpoint).Assembly.DefinedTypes .Where(t => t.IsClass) .SelectMany(t => t.GetMethods(BindingFlags.Static | BindingFlags.Public)) .Where(m => m.CustomAttributes.Any(a => a.AttributeType == typeof(FunctionNameAttribute))) .Select(m => m.Name) .ToHashSet(); // Only these two methods should be publicly accessible as of today functionsToBeCalled.Remove(nameof(ServeStatics.DfmServeStaticsFunction)); functionsToBeCalled.Remove(nameof(EasyAuthConfig.DfmGetEasyAuthConfigFunction)); // Collecting the list of functions that were actually called by this test var functionsThatWereCalled = new HashSet <string>(); logMoq.Setup(log => log.Log(It.IsAny <LogLevel>(), It.IsAny <EventId>(), It.IsAny <It.IsAnyType>(), It.IsAny <Exception>(), It.IsAny <Func <It.IsAnyType, Exception, string> >())) .Callback((LogLevel l, EventId i, object s, Exception ex, object o) => { // Ensuring the correct type of exception was raised internally Assert.IsInstanceOfType(ex, typeof(UnauthorizedAccessException)); Assert.AreEqual("No access token provided. Call is rejected.", ex.Message); // Also extracting the function name, that was called, from current stack trace foreach (var stackFrame in new StackTrace().GetFrames()) { var method = stackFrame.GetMethod(); if (method.CustomAttributes.Any(a => a.AttributeType == typeof(FunctionNameAttribute))) { functionsThatWereCalled.Add(method.Name); } } }); Environment.SetEnvironmentVariable(EnvVariableNames.DFM_HUB_NAME, string.Empty); // Act var results = new List <IActionResult>() { await About.DfmAboutFunction(request, "-", "TestHub", logMoq.Object), await new CleanEntityStorage(null).DfmCleanEntityStorageFunction(request, durableClientMoq.Object, "-", "TestHub", logMoq.Object), await new DeleteTaskHub(null).DfmDeleteTaskHubFunction(request, durableClientMoq.Object, "-", "TestHub", logMoq.Object), await new IdSuggestions(null).DfmGetIdSuggestionsFunction(request, durableClientMoq.Object, "-", "TestHub", "abc", logMoq.Object), await ManageConnection.DfmManageConnectionFunction(request, "-", "TestHub", new Microsoft.Azure.WebJobs.ExecutionContext(), logMoq.Object), await new IdSuggestions(null).DfmGetIdSuggestionsFunction(request, durableClientMoq.Object, "-", "TestHub", "abc", logMoq.Object), await new Orchestration(null).DfmGetOrchestrationFunction(request, durableClientMoq.Object, "-", "TestHub", "abc", logMoq.Object), await new Orchestration(null).DfmGetOrchestrationHistoryFunction(request, durableClientMoq.Object, "-", "TestHub", "abc", logMoq.Object), await new Orchestration(null).DfmStartNewOrchestrationFunction(request, durableClientMoq.Object, "-", "TestHub", logMoq.Object), await new Orchestration(null).DfmPostOrchestrationFunction(request, durableClientMoq.Object, "-", "TestHub", "abc", "todo", logMoq.Object), await new Orchestration(null).DfmGetOrchestrationTabMarkupFunction(request, durableClientMoq.Object, "-", "TestHub", "abc", "todo", logMoq.Object), await new Orchestrations(null).DfmGetOrchestrationsFunction(request, durableClientMoq.Object, "-", "TestHub", logMoq.Object), await new PurgeHistory(null).DfmPurgeHistoryFunction(request, durableClientMoq.Object, "-", "TestHub", logMoq.Object), await TaskHubNames.DfmGetTaskHubNamesFunction(request, logMoq.Object), await FunctionMap.DfmGetFunctionMap(request, "-", "TestHub", logMoq.Object), }; // Assert results.ForEach(r => Assert.IsInstanceOfType(r, typeof(UnauthorizedResult))); functionsToBeCalled.ExceptWith(functionsThatWereCalled); Assert.IsTrue(functionsToBeCalled.Count == 0, "You forgot to test " + string.Join(", ", functionsToBeCalled)); }