public async Task <IHttpActionResult> PostTopicName([FromBody] PostTopicNameRequest request) { string className = "TopicsController"; string methodName = "PostTopicName"; string logEntry = $"PublisherType = {request?.PublisherType}, TopicHandle = {request?.TopicHandle}"; this.LogControllerStart(this.log, className, methodName, logEntry); var userProfileEntity = await this.usersManager.ReadUserProfile(this.UserHandle, this.AppHandle); if (userProfileEntity == null) { return(this.Unauthorized(ResponseStrings.UserNotFound)); } // app published topic names can only be created by an administrator of that application if (request.PublisherType == PublisherType.App) { bool isAdmin = await this.appsManager.IsAdminUser(this.AppHandle, this.UserHandle); if (!isAdmin) { return(this.Unauthorized(ResponseStrings.UserUnauthorized)); } } // currently, we only support app published topic names if (request.PublisherType != PublisherType.App) { return(this.NotImplemented(ResponseStrings.NotImplemented)); } // if the topic handle does not exist, return an error that indicates the topic was not found var topicEntity = await this.topicsManager.ReadTopic(request.TopicHandle); if (topicEntity == null) { return(this.NotFound(ResponseStrings.TopicNotFound)); } // check if the requested topic name is already in use for this app var topicNameEntity = await this.topicNamesManager.ReadTopicName(this.AppHandle, request.TopicName); if (topicNameEntity != null) { return(this.Conflict(ResponseStrings.ItemExists)); } await this.topicNamesManager.InsertTopicName( ProcessType.Frontend, this.AppHandle, request.TopicName, request.TopicHandle); this.LogControllerEnd(this.log, className, methodName, logEntry); return(this.NoContent()); }
public async Task CreateVerifyDeleteNamedTopicTest() { HttpOperationResponse <object> deleteUserOperationResponse = null; HttpOperationResponse <object> deleteTopicOperationResponse = null; HttpOperationResponse <object> deleteTopicNameOperationResponse = null; DeleteTopicNameRequest deleteTopicNameReq = new DeleteTopicNameRequest(publisherType: PublisherType.App); int endIndex = TestConstants.ConfigFileName.IndexOf("."); this.environment = TestConstants.ConfigFileName.Substring(0, endIndex); // create a user SocialPlusClient client = new SocialPlusClient(TestConstants.ServerApiBaseUrl); var t = await this.CreateUserForTest(client); PostUserResponse postUserResponse = t.Item1; string auth = t.Item2; string userHandle = postUserResponse.UserHandle; string appHandle = ManageAppsUtils.GetAppHandle(this.environment); if (appHandle == null) { // delete the user and fail the test deleteUserOperationResponse = await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth); Assert.Fail("Failed to lookup appHandle"); } // add user as admin bool added = ManageAppsUtils.AddAdmin(this.environment, appHandle, userHandle); if (!added) { // delete the user and fail the test deleteUserOperationResponse = await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth); Assert.Fail("Failed to set user as administrator"); } // create a topic string topicTitle = "Test topic for named topics"; string topicText = "This sure is a fine topic."; BlobType blobType = BlobType.Image; string blobHandle = "http://myBlobHandle/"; string language = "en-US"; string group = "mygroup"; PostTopicRequest postTopicRequest = new PostTopicRequest(publisherType: PublisherType.User, text: topicText, title: topicTitle, blobType: blobType, blobHandle: blobHandle, language: language, group: group); HttpOperationResponse <PostTopicResponse> postTopicOperationResponse = await client.Topics.PostTopicWithHttpMessagesAsync(request : postTopicRequest, authorization : auth); // if create topic was a success, grab the topic handle string topicHandle = string.Empty; if (postTopicOperationResponse.Response.IsSuccessStatusCode) { topicHandle = postTopicOperationResponse.Body.TopicHandle; } else { // otherwise, delete the admin, the user and fail the test ManageAppsUtils.DeleteAdmin(this.environment, appHandle, userHandle); deleteUserOperationResponse = await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth); Assert.Fail("Create topic failed, cannot finish the CreateVerifyUpdateDeleteNamedTopicTest"); } string topicName = "UnitTestTopicName"; // create a topic name PostTopicNameRequest postTopicNameReq = new PostTopicNameRequest(publisherType: PublisherType.App, topicName: topicName, topicHandle: topicHandle); HttpOperationResponse <object> postTopicNameOperationResponse = await client.Topics.PostTopicNameWithHttpMessagesAsync(request : postTopicNameReq, authorization : auth); // if creating the topic name fails, delete the topic, the user, and fail the test if (!postTopicNameOperationResponse.Response.IsSuccessStatusCode) { deleteTopicOperationResponse = await client.Topics.DeleteTopicWithHttpMessagesAsync(authorization : auth, topicHandle : topicHandle); ManageAppsUtils.DeleteAdmin(this.environment, appHandle, userHandle); deleteUserOperationResponse = await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth); Assert.Fail("Create topic name failed, cannot finish the CreateVerifyUpdateDeleteNamedTopicTest"); } // get the topic name HttpOperationResponse <GetTopicByNameResponse> getTopicNameResponse = await client.Topics.GetTopicByNameWithHttpMessagesAsync(topicName : topicName, publisherType : PublisherType.App, authorization : auth); if (!getTopicNameResponse.Response.IsSuccessStatusCode) { // if get topic name fails, cleanup: delete the topic name, the admin, the user, and fail the test deleteTopicNameOperationResponse = await client.Topics.DeleteTopicNameWithHttpMessagesAsync(request : deleteTopicNameReq, authorization : auth, topicName : topicName); deleteTopicOperationResponse = await client.Topics.DeleteTopicWithHttpMessagesAsync(authorization : auth, topicHandle : topicHandle); ManageAppsUtils.DeleteAdmin(this.environment, appHandle, userHandle); deleteUserOperationResponse = await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth); Assert.Fail("get topic name failed, cannot finish the CreateVerifyUpdateDeleteNamedTopicTest"); } // delete the topic name we just created deleteTopicNameOperationResponse = await client.Topics.DeleteTopicNameWithHttpMessagesAsync(request : deleteTopicNameReq, authorization : auth, topicName : topicName); // if deleting the topic name fails, delete the topic, the admin, the user, and fail the test if (!deleteTopicNameOperationResponse.Response.IsSuccessStatusCode) { deleteTopicOperationResponse = await client.Topics.DeleteTopicWithHttpMessagesAsync(authorization : auth, topicHandle : topicHandle); ManageAppsUtils.DeleteAdmin(this.environment, appHandle, userHandle); deleteUserOperationResponse = await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth); Assert.Fail("Delete topic name failed, cannot finish the CreateVerifyUpdateDeleteNamedTopicTest"); } // do standard cleanup : delete the topic, delete the admin, and then delete the user deleteTopicOperationResponse = await client.Topics.DeleteTopicWithHttpMessagesAsync(authorization : auth, topicHandle : topicHandle); ManageAppsUtils.DeleteAdmin(this.environment, appHandle, userHandle); deleteUserOperationResponse = await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth); // if we reach here, the test was successful. return; }
public async Task CreateUpdateDeleteNamedTopicTest() { var deleteRequest = new DeleteTopicNameRequest() { PublisherType = PublisherType.App }; int endIndex = TestConstants.ConfigFileName.IndexOf("."); this.environment = TestConstants.ConfigFileName.Substring(0, endIndex); // create a user SocialPlusClient client = new SocialPlusClient(TestConstants.ServerApiBaseUrl); var t = await this.CreateUserForTest(client); PostUserResponse postUserResponse = t.Item1; string auth = t.Item2; string userHandle = postUserResponse.UserHandle; // get the app handle string appHandle = ManageAppsUtils.GetAppHandle(this.environment); if (appHandle == null) { // delete the user and fail the test await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth); Assert.Fail("Failed to lookup appHandle"); } // add user as admin bool added = ManageAppsUtils.AddAdmin(this.environment, appHandle, userHandle); if (!added) { // delete the user and fail the test await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth); Assert.Fail("Failed to set user as administrator"); } // create a topic string topicTitle = "Test topic for named topics"; string topicText = "This sure is a fine topic."; BlobType blobType = BlobType.Image; string blobHandle = "http://myBlobHandle/"; string language = "en-US"; string group = "mygroup"; string topicHandle = null; PostTopicRequest postTopicRequest = new PostTopicRequest(publisherType: PublisherType.User, text: topicText, title: topicTitle, blobType: blobType, blobHandle: blobHandle, language: language, group: group); HttpOperationResponse <PostTopicResponse> postTopicOperationResponse = await client.Topics.PostTopicWithHttpMessagesAsync(request : postTopicRequest, authorization : auth); if (postTopicOperationResponse.Response.IsSuccessStatusCode) { topicHandle = postTopicOperationResponse.Body.TopicHandle; } // create another topic string topicTitle2 = "Test topic #2 for named topics"; string topicText2 = "This one also sure is a fine topic."; string language2 = "en-US"; string group2 = "mygroup2"; string topicHandle2 = null; PostTopicRequest postTopicRequest2 = new PostTopicRequest(publisherType: PublisherType.User, text: topicText2, title: topicTitle2, language: language2, group: group2); HttpOperationResponse <PostTopicResponse> postTopicOperationResponse2 = await client.Topics.PostTopicWithHttpMessagesAsync(request : postTopicRequest2, authorization : auth); if (postTopicOperationResponse2.Response.IsSuccessStatusCode) { topicHandle2 = postTopicOperationResponse2.Body.TopicHandle; } if (!(postTopicOperationResponse.Response.IsSuccessStatusCode && postTopicOperationResponse2.Response.IsSuccessStatusCode)) { // if either topic creation fails, cleanup: // delete both topics, the admin, the user and fail the test await client.Topics.DeleteTopicWithHttpMessagesAsync(authorization : auth, topicHandle : topicHandle); await client.Topics.DeleteTopicWithHttpMessagesAsync(authorization : auth, topicHandle : topicHandle2); ManageAppsUtils.DeleteAdmin(this.environment, appHandle, userHandle); await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth); Assert.Fail("Create topics failed, cannot finish the CreateVerifyUpdateDeleteNamedTopicTest"); } // create a topic name for topic #1 string topicName = "SpecialTopicName"; PostTopicNameRequest postTopicNameReq = new PostTopicNameRequest(publisherType: PublisherType.App, topicName: topicName, topicHandle: topicHandle); HttpOperationResponse <object> postTopicNameOperationResponse = await client.Topics.PostTopicNameWithHttpMessagesAsync(request : postTopicNameReq, authorization : auth); if (!postTopicNameOperationResponse.Response.IsSuccessStatusCode) { // if creating the topic name fails, cleanup: // delete both topics, the admin, the user, and fail the test await client.Topics.DeleteTopicWithHttpMessagesAsync(authorization : auth, topicHandle : topicHandle); await client.Topics.DeleteTopicWithHttpMessagesAsync(authorization : auth, topicHandle : topicHandle2); ManageAppsUtils.DeleteAdmin(this.environment, appHandle, userHandle); await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth); Assert.Fail("Create topic name failed, cannot finish the CreateVerifyUpdateDeleteNamedTopicTest"); } // update the topic name so that it now refers to topic #2 PutTopicNameRequest putTopicNameReq = new PutTopicNameRequest(publisherType: PublisherType.App, topicHandle: topicHandle2); HttpOperationResponse <object> putTopicNameOperationResponse = await client.Topics.PutTopicNameWithHttpMessagesAsync(topicName : topicName, request : putTopicNameReq, authorization : auth); if (!putTopicNameOperationResponse.Response.IsSuccessStatusCode) { // if updating the topic name fails, cleanup await client.Topics.DeleteTopicNameWithHttpMessagesAsync(topicName : topicName, request : deleteRequest, authorization : auth); await client.Topics.DeleteTopicWithHttpMessagesAsync(authorization : auth, topicHandle : topicHandle); await client.Topics.DeleteTopicWithHttpMessagesAsync(authorization : auth, topicHandle : topicHandle2); ManageAppsUtils.DeleteAdmin(this.environment, appHandle, userHandle); await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth); Assert.Fail("Put topic name failed, cannot finish the CreateVerifyUpdateDeleteNamedTopicTest"); } // do the standard cleanup await client.Topics.DeleteTopicNameWithHttpMessagesAsync(topicName : topicName, request : deleteRequest, authorization : auth); await client.Topics.DeleteTopicWithHttpMessagesAsync(authorization : auth, topicHandle : topicHandle); await client.Topics.DeleteTopicWithHttpMessagesAsync(authorization : auth, topicHandle : topicHandle2); ManageAppsUtils.DeleteAdmin(this.environment, appHandle, userHandle); await client.Users.DeleteUserWithHttpMessagesAsync(authorization : auth); }
/// <summary> /// Publish a new app-published topic with a topic name to Embedded Social /// </summary> /// <param name="topicName">name of the new topic</param> /// <param name="topicTitle">title of the new topic</param> /// <param name="topicText">text of the new topic</param> /// <param name="topicCategory">category of the new topic</param> /// <returns>task that publishes a new topic</returns> private async Task CreateTopic(string topicName, string topicTitle, string topicText, string topicCategory) { // format the input into an Embedded Social topic request PostTopicRequest topicRequest = new PostTopicRequest() { PublisherType = PublisherType.App, Text = topicText, Title = topicTitle, BlobType = BlobType.Unknown, BlobHandle = string.Empty, Categories = topicCategory, Language = TopicLanguage, DeepLink = string.Empty, FriendlyName = topicName, Group = string.Empty }; // publish to Embedded Social HttpOperationResponse <PostTopicResponse> postTopicOperationResponse = await this.client.Topics.PostTopicWithHttpMessagesAsync(request : topicRequest, authorization : this.authorization); // check response if (postTopicOperationResponse == null || postTopicOperationResponse.Response == null) { throw new Exception("got null response"); } else if (!postTopicOperationResponse.Response.IsSuccessStatusCode) { throw new Exception("request failed with HTTP code: " + postTopicOperationResponse.Response.StatusCode + ", and reason: " + postTopicOperationResponse.Response.ReasonPhrase); } else if (postTopicOperationResponse.Body == null) { throw new Exception("got null response body"); } else if (string.IsNullOrWhiteSpace(postTopicOperationResponse.Body.TopicHandle)) { throw new Exception("topicHandle is null or whitespace"); } // publish the name for the topic string topicHandle = postTopicOperationResponse.Body.TopicHandle; PostTopicNameRequest topicNameRequest = new PostTopicNameRequest() { PublisherType = PublisherType.App, TopicName = topicName, TopicHandle = topicHandle }; HttpOperationResponse postTopicNameOperationResponse = await this.client.Topics.PostTopicNameWithHttpMessagesAsync(request : topicNameRequest, authorization : this.authorization); // check response if (postTopicNameOperationResponse == null || postTopicNameOperationResponse.Response == null) { // attempt to clean up the topic first HttpOperationResponse deleteTopicOperationResponse = await this.client.Topics.DeleteTopicWithHttpMessagesAsync(topicHandle : topicHandle, authorization : this.authorization); throw new Exception("got null response"); } else if (!postTopicNameOperationResponse.Response.IsSuccessStatusCode) { // attempt to clean up the topic first HttpOperationResponse deleteTopicOperationResponse = await this.client.Topics.DeleteTopicWithHttpMessagesAsync(topicHandle : topicHandle, authorization : this.authorization); throw new Exception("request failed with HTTP code: " + postTopicNameOperationResponse.Response.StatusCode + ", and reason: " + postTopicNameOperationResponse.Response.ReasonPhrase); } }
/// <summary> /// Create a topic name /// </summary> /// <param name='operations'> /// The operations group for this extension method. /// </param> /// <param name='request'> /// Post topic name request /// </param> /// <param name='authorization'> /// Format is: "Scheme CredentialsList". Possible values are: /// /// - Anon AK=AppKey /// /// - SocialPlus TK=SessionToken /// /// - Facebook AK=AppKey|TK=AccessToken /// /// - Google AK=AppKey|TK=AccessToken /// /// - Twitter AK=AppKey|RT=RequestToken|TK=AccessToken /// /// - Microsoft AK=AppKey|TK=AccessToken /// /// - AADS2S AK=AppKey|[UH=UserHandle]|TK=AADToken /// </param> /// <param name='cancellationToken'> /// The cancellation token. /// </param> public static async Task <object> PostTopicNameAsync(this ITopics operations, PostTopicNameRequest request, string authorization, CancellationToken cancellationToken = default(CancellationToken)) { using (var _result = await operations.PostTopicNameWithHttpMessagesAsync(request, authorization, null, cancellationToken).ConfigureAwait(false)) { return(_result.Body); } }
/// <summary> /// Create a topic name /// </summary> /// <param name='operations'> /// The operations group for this extension method. /// </param> /// <param name='request'> /// Post topic name request /// </param> /// <param name='authorization'> /// Format is: "Scheme CredentialsList". Possible values are: /// /// - Anon AK=AppKey /// /// - SocialPlus TK=SessionToken /// /// - Facebook AK=AppKey|TK=AccessToken /// /// - Google AK=AppKey|TK=AccessToken /// /// - Twitter AK=AppKey|RT=RequestToken|TK=AccessToken /// /// - Microsoft AK=AppKey|TK=AccessToken /// /// - AADS2S AK=AppKey|[UH=UserHandle]|TK=AADToken /// </param> public static object PostTopicName(this ITopics operations, PostTopicNameRequest request, string authorization) { return(Task.Factory.StartNew(s => ((ITopics)s).PostTopicNameAsync(request, authorization), operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult()); }