Esempio n. 1
0
        /// <summary>
        /// Delete a topic from Embedded Social
        /// </summary>
        /// <param name="topicName">name of the existing topic</param>
        /// <returns>task that deletes the topic name and the topic</returns>
        private async Task DeleteTopic(string topicName)
        {
            // get the handle for the topic name
            HttpOperationResponse <GetTopicByNameResponse> getTopicByNameOperationResponse = await this.client.Topics.GetTopicByNameWithHttpMessagesAsync(topicName : topicName, publisherType : PublisherType.App, authorization : this.authorization);

            // check response
            if (getTopicByNameOperationResponse == null || getTopicByNameOperationResponse.Response == null)
            {
                throw new Exception("got null response");
            }
            else if (!getTopicByNameOperationResponse.Response.IsSuccessStatusCode)
            {
                throw new Exception("request failed with HTTP code: " + getTopicByNameOperationResponse.Response.StatusCode + ", and reason: " + getTopicByNameOperationResponse.Response.ReasonPhrase);
            }
            else if (getTopicByNameOperationResponse.Body == null)
            {
                throw new Exception("got null response body");
            }
            else if (string.IsNullOrWhiteSpace(getTopicByNameOperationResponse.Body.TopicHandle))
            {
                throw new Exception("topicHandle is null or whitespace");
            }

            // delete the topic
            string topicHandle = getTopicByNameOperationResponse.Body.TopicHandle;
            HttpOperationResponse deleteTopicOperationResponse = await this.client.Topics.DeleteTopicWithHttpMessagesAsync(topicHandle : topicHandle, authorization : this.authorization);

            // check response
            if (deleteTopicOperationResponse == null || deleteTopicOperationResponse.Response == null)
            {
                throw new Exception("got null response");
            }
            else if (!deleteTopicOperationResponse.Response.IsSuccessStatusCode)
            {
                throw new Exception("request failed with HTTP code: " + deleteTopicOperationResponse.Response.StatusCode + ", and reason: " + deleteTopicOperationResponse.Response.ReasonPhrase);
            }

            // delete the topic name
            DeleteTopicNameRequest deleteTopicNameRequest = new DeleteTopicNameRequest()
            {
                PublisherType = PublisherType.App
            };
            HttpOperationResponse deleteTopicNameOperationResponse = await this.client.Topics.DeleteTopicNameWithHttpMessagesAsync(topicName : topicName, request : deleteTopicNameRequest, authorization : this.authorization);

            // check response
            if (deleteTopicNameOperationResponse == null || deleteTopicNameOperationResponse.Response == null)
            {
                throw new Exception("got null response");
            }
            else if (!deleteTopicNameOperationResponse.Response.IsSuccessStatusCode)
            {
                throw new Exception("request failed with HTTP code: " + deleteTopicNameOperationResponse.Response.StatusCode + ", and reason: " + deleteTopicNameOperationResponse.Response.ReasonPhrase);
            }
        }
Esempio n. 2
0
        public async Task <IHttpActionResult> DeleteTopicName(string topicName, [FromBody] DeleteTopicNameRequest request)
        {
            string className  = "TopicsController";
            string methodName = "DeleteTopicName";
            string logEntry   = $"PublisherType = {request?.PublisherType}";

            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 deleted 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));
            }

            // find the topic name to delete
            var topicNameEntity = await this.topicNamesManager.ReadTopicName(this.AppHandle, topicName);

            if (topicNameEntity == null)
            {
                return(this.NotFound(ResponseStrings.TopicNameNotFound));
            }

            await this.topicNamesManager.DeleteTopicName(
                ProcessType.Frontend,
                this.AppHandle,
                topicName);

            logEntry += $", TopicHandle = {topicNameEntity?.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);
        }
Esempio n. 5
0
 /// <summary>
 /// Delete a topic name
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='topicName'>
 /// Topic name
 /// </param>
 /// <param name='request'>
 /// Delete topic 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> DeleteTopicNameAsync(this ITopics operations, string topicName, DeleteTopicNameRequest request, string authorization, CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.DeleteTopicNameWithHttpMessagesAsync(topicName, request, authorization, null, cancellationToken).ConfigureAwait(false))
     {
         return(_result.Body);
     }
 }
Esempio n. 6
0
 /// <summary>
 /// Delete a topic name
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='topicName'>
 /// Topic name
 /// </param>
 /// <param name='request'>
 /// Delete topic 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 DeleteTopicName(this ITopics operations, string topicName, DeleteTopicNameRequest request, string authorization)
 {
     return(Task.Factory.StartNew(s => ((ITopics)s).DeleteTopicNameAsync(topicName, request, authorization), operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult());
 }