コード例 #1
0
        public void ControlledUpdate_EntityWithMigrationLevelAuthorization_OverrideAnyLevel()
        {
            //ARRANGE
            const string higherContext = "HigherUser";
            var          auth          = new Dictionary <string, string> {
                { "Name", higherContext }
            };
            string authXml = SerialisationUtils.ToXml(auth);
            ControlledUpdateThing thing
                = new ControlledUpdateThing
                {
                DbName = "Test Thing",
                DbPropertyAuthorization = authXml
                };
            const string newName = "New Thing";

            //ACT
            using (PebblecodeUpdateContexts.Migration(thing))
            {
                thing.Name = newName;
            }

            //ASSERT
            Assert.AreEqual(newName, thing.Name);
        }
コード例 #2
0
        public void ControlledUpdate_EntityWithMigrationLevelAuthorization_KeepUpdateLevelUnchanged()
        {
            //ARRANGE
            const string previousHigherContext = "HigherUser";
            var          auth = new Dictionary <string, string> {
                { "Name", previousHigherContext }
            };
            string authXml = SerialisationUtils.ToXml(auth);
            ControlledUpdateThing thing
                = new ControlledUpdateThing
                {
                DbName = "Test Thing",
                DbPropertyAuthorization = authXml
                };
            const string newName = "New Thing";

            //ACT
            using (PebblecodeUpdateContexts.Migration(thing))
            {
                thing.Name = newName;
            }
            Dictionary <string, string> currentAuth =
                SerialisationUtils.FromXml <Dictionary <string, string> >(thing.DbPropertyAuthorization);

            //ASSERT
            Assert.IsTrue(currentAuth.ContainsKey("Name"));
            Assert.AreEqual(previousHigherContext, currentAuth["Name"]);
        }
コード例 #3
0
 private void WriteBody(StreamWriter writer, ExampleStore exampleStore, ResourceNode node, TDataExchangeFormat dataExchangeFormat, string contentType, TMessageType messageType, Type bodyObjectType, int indent)
 {
     writer.WriteLine(string.Concat(GetIndentString(indent), contentType, ":"));
     if (SerialisationUtils.IsStandardDataExchangeFormat(dataExchangeFormat))
     {
         writer.WriteLine(string.Concat(GetIndentString(indent + 1), "schema: ", GetSchemaKey(bodyObjectType, dataExchangeFormat)));
     }
     WriteExample(writer, exampleStore, node.Class, node.Method, contentType, messageType, dataExchangeFormat, indent + 1);
 }
コード例 #4
0
        private void WriteDefaultDescription(StreamWriter writer, DocumentationHeaderSettings headerSettings, HttpStatusCode statusCode, int indent)
        {
            writer.WriteLine(string.Concat(GetIndentString(indent), "description: |"));
            writer.WriteLine(string.Concat(GetIndentString(indent + 1), SerialisationUtils.PrettifyHttpStatusCode(statusCode)));
            string defaultDescription = null;

            headerSettings.DefaultStatusDescriptions.TryGetValue(((int)statusCode).ToString(), out defaultDescription);
            if (defaultDescription != null)
            {
                writer.WriteLine(string.Concat(GetIndentString(indent + 1), defaultDescription));
            }
            else
            {
                SerialisationLog.Warning(string.Concat("No default description for Http Status Code ", (int)statusCode, ":", statusCode));
            }
        }
コード例 #5
0
        private void WriteSchemas(StreamWriter writer, SchemaStore schemaStore)
        {
            writer.WriteLine("schemas: ");

            foreach (Schema schema in schemaStore.Schemas.Values)
            {
                foreach (KeyValuePair <TDataExchangeFormat, string> pair in schema.Content)
                {
                    writer.WriteLine(string.Concat(GetIndentString(1), "- ", GetSchemaKey(schema.Object, pair.Key), ": |"));
                    List <string> lines = SerialisationUtils.SplitLines(pair.Value);
                    foreach (string line in lines)
                    {
                        writer.WriteLine(string.Concat(GetIndentString(3), line));
                    }
                }
            }
        }
コード例 #6
0
        private void WriteExample(StreamWriter writer, ExampleStore exampleStore, Type classType, MethodInfo methodInfo, string contentType, TMessageType exampleType, TDataExchangeFormat dataExchangeFormat, int indent)
        {
            string exampleText = exampleStore.GetExampleContent(classType, methodInfo, exampleType, contentType, dataExchangeFormat);

            if (exampleText != null)
            {
                writer.WriteLine(string.Concat(GetIndentString(indent), "example: |"));
                List <string> lines = SerialisationUtils.SplitLines(exampleText);
                foreach (string line in lines)
                {
                    writer.WriteLine(string.Concat(GetIndentString(indent + 1), line));
                }
            }
            else if (dataExchangeFormat != TDataExchangeFormat.Xml)
            {
                SerialisationLog.Warning(string.Concat("No example for ", classType.Name, ".", methodInfo.Name, " ", dataExchangeFormat, " ", exampleType.ToString().ToLower()));
            }
        }
コード例 #7
0
        private string GetBasicAuthXml()
        {
            var auth = new Dictionary <string, string>();

            return(SerialisationUtils.ToXml(auth));
        }
コード例 #8
0
        private void WriteMethod(StreamWriter writer, ResourceNode node, DocumentationHeaderSettings headerSettings, SchemaStore schemaStore, ExampleStore exampleStore, int indent)
        {
            MethodDocumentationAttribute attribute = node.Method.GetCustomAttributes <MethodDocumentationAttribute>().FirstOrDefault();

            if (node.AllowsAnonymous())
            {
                string securitySchemes = "null";
                if (attribute != null && attribute.AllowMultipleSecuritySchemes)
                {
                    securitySchemes = string.Concat(securitySchemes, ", oauth_2_0");
                }
                writer.WriteLine(string.Concat(GetIndentString(indent + 1), "securedBy: [", securitySchemes, "]"));
            }

            if (attribute != null)
            {
                if (attribute.Summary != null)
                {
                    writer.WriteLine(string.Concat(GetIndentString(indent + 1), "description: |"));
                    writer.WriteLine(string.Concat(GetIndentString(indent + 2), attribute.Summary));
                    Example example = exampleStore.GetExample(node.Class, node.Method, "");
                    if (example != null)
                    {
                        writer.WriteLine(GetIndentString(indent + 2));
                        writer.WriteLine(string.Concat(GetIndentString(indent + 2), "For more information, go to ", headerSettings.RepositoryFilesURI, "/", example.DocFilename, "#", example.DocHeading));
                    }
                    else if (attribute.RequestTypes != null)
                    {
                        SerialisationLog.Warning(string.Concat("No example to retrieve link to more information for ", node.Class.Name, ".", node.Method.Name));
                    }
                }
                else
                {
                    SerialisationLog.Warning(string.Concat("No summary for ", node.Class.Name, ".", node.Method.Name));
                }

                if (attribute.ResponseTypes != null && attribute.ResponseTypes.Any(r => r.GetProperties().Any(p => p.PropertyType == typeof(PageInfo))))
                {
                    writer.WriteLine(string.Concat(GetIndentString(indent + 1), "queryParameters: "));
                    foreach (KeyValuePair <string, Dictionary <string, string> > field in headerSettings.PagingFields)
                    {
                        writer.WriteLine(string.Concat(GetIndentString(indent + 2), field.Key, ":"));
                        writer.WriteLine(string.Concat(GetIndentString(indent + 3), "displayName: ", field.Value["displayName"]));
                        writer.WriteLine(string.Concat(GetIndentString(indent + 3), "description: ", field.Value["description"]));
                        writer.WriteLine(string.Concat(GetIndentString(indent + 3), "type: ", field.Value["type"]));
                        writer.WriteLine(string.Concat(GetIndentString(indent + 3), "required: ", field.Value["required"]));
                    }
                }

                if (attribute.RequestTypes != null)
                {
                    writer.WriteLine(string.Concat(GetIndentString(indent + 1), "body: "));

                    foreach (Type requestType in attribute.RequestTypes)
                    {
                        ContentTypeAttribute contentTypeAttribute = requestType.GetCustomAttributes <ContentTypeAttribute>().FirstOrDefault();
                        if (contentTypeAttribute == null)
                        {
                            SerialisationLog.Warning(string.Concat("No ContentTypeAttribute for ", requestType.FullName));
                        }

                        if (attribute.RequestTypeNames != null)
                        {
                            foreach (string contentType in attribute.RequestTypeNames)
                            {
                                TDataExchangeFormat dataExchangeFormat = SerialisationUtils.GetDataExchangeFormatFromContentType(contentType);

                                if (dataExchangeFormat != TDataExchangeFormat.None)
                                {
                                    WriteBody(writer, exampleStore, node, dataExchangeFormat, contentType, TMessageType.Request, requestType, indent + 2);
                                }
                                else
                                {
                                    SerialisationLog.Warning(string.Concat("No supported data exchange format for ", contentType));
                                }
                            }
                        }
                        else
                        {
                            foreach (TDataExchangeFormat dataExchangeFormat in Enum.GetValues(typeof(TDataExchangeFormat)))
                            {
                                if (SerialisationUtils.IsStandardDataExchangeFormat(dataExchangeFormat))
                                {
                                    string contentType = GetContentType(contentTypeAttribute, dataExchangeFormat);
                                    WriteBody(writer, exampleStore, node, dataExchangeFormat, contentType, TMessageType.Request, requestType, indent + 2);
                                }
                            }
                        }
                    }
                }

                writer.WriteLine(string.Concat(GetIndentString(indent + 1), "responses:"));

                HttpStatusCode[] statusCodes = attribute.StatusCodes;
                if (node.Class.GetCustomAttributes <AuthorizeAttribute>().FirstOrDefault() != null)
                {
                    statusCodes = statusCodes.Concat(new[] { HttpStatusCode.Unauthorized, HttpStatusCode.Forbidden }).ToArray();
                }
                foreach (HttpStatusCode statusCode in statusCodes)
                {
                    writer.WriteLine(string.Concat(GetIndentString(indent + 2), (int)statusCode, ":"));
                    WriteDefaultDescription(writer, headerSettings, statusCode, indent + 3);

                    if (SerialisationUtils.IsSuccessStatusCode(statusCode) && attribute.ResponseTypes != null)
                    {
                        writer.WriteLine(string.Concat(GetIndentString(indent + 3), "body:"));
                        foreach (Type responseType in attribute.ResponseTypes)
                        {
                            ContentTypeAttribute contentTypeAttribute = responseType.GetCustomAttributes <ContentTypeAttribute>().FirstOrDefault();
                            if (contentTypeAttribute == null)
                            {
                                SerialisationLog.Warning(string.Concat("No ContentTypeAttribute for ", responseType.FullName));
                            }

                            foreach (TDataExchangeFormat dataExchangeFormat in Enum.GetValues(typeof(TDataExchangeFormat)))
                            {
                                if (SerialisationUtils.IsStandardDataExchangeFormat(dataExchangeFormat))
                                {
                                    string contentType = GetContentType(contentTypeAttribute, dataExchangeFormat);
                                    WriteBody(writer, exampleStore, node, dataExchangeFormat, contentType, TMessageType.Response, responseType, indent + 4);
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                SerialisationLog.Warning(string.Concat("No method-level documentation for ", node.Class.Name, ".", node.Method.Name));
            }
        }