From dafc815be5ff58962f310f0b37633ce2762cfd59 Mon Sep 17 00:00:00 2001 From: Cubicroot Date: Sun, 20 Jun 2021 17:39:10 +0200 Subject: [PATCH] move errors to new package to break import cycle --- app/test_notifications.py | 168 ++++++++++++++++++++++++++++ execute_tests.sh | 2 +- go.sum | 11 ++ internal/api/errors.go | 5 - internal/api/util.go | 3 +- internal/dispatcher/notification.go | 12 +- internal/pberrors/errors.go | 6 + tests/mockups/dispatcher.go | 8 +- 8 files changed, 195 insertions(+), 20 deletions(-) create mode 100644 app/test_notifications.py delete mode 100644 internal/api/errors.go create mode 100644 internal/pberrors/errors.go diff --git a/app/test_notifications.py b/app/test_notifications.py new file mode 100644 index 0000000..c12d5e4 --- /dev/null +++ b/app/test_notifications.py @@ -0,0 +1,168 @@ +import requests, json + +token = 'AxZTwBZXs2YK1J5IZa0fyCcfHz9cYM9VjaO627eMSbAXAMdwFluGZSOTB4Vk3RHY' + +url = 'https://push.remote.alexanderebhart.de/message?token=' + token +headers = {'content-type': 'application/json'} + +messages = [ + { + "message": "
", + "title": "NEW TEST", + "extras": + { + "client::display": + { + "contentType": "text/html" + } + } + }, + { + "message": "line
break", + "title": "Linebreak single br", + "extras": + { + "client::display": + { + "contentType": "text/html" + } + } + }, + { + "message": "line \n break", + "title": "Linebreak single n", + "extras": + { + "client::display": + { + "contentType": "text/html" + } + } + }, + { + "message": """line + break""", + "title": "Linebreak native", + "extras": + { + "client::display": + { + "contentType": "text/html" + } + } + }, + { + "message": "# Heading 1\n\n###### Heading 6\n\nAlt-H1\n=====\n\n**bold**\n\n_curly_\n\n~~Strike through~~\n\n[Link](google.com)\n\n* list1 \n* list2\n\n1. another list\n2. numerated\n\n![alt text](https://github.com/adam-p/markdown-here/raw/master/src/common/images/icon48.png)\n\n`some inline code`\n\n```\nA code block\n```\n\n> Blockquote", + "title": "Markdown styling", + "extras": + { + "client::display": + { + "contentType": "text/markdown" + } + } + }, + { + "message": "Red", + "title": "HTML", + "extras": + { + "client::display": + { + "contentType": "text/html" + } + } + }, +] + +priorities = [ + { + "message": "Some text", + "title": "Prio 0", + "extras": + { + "client::display": + { + "contentType": "text/html" + } + }, + "priority": 0 + }, + { + "message": "Some text", + "title": "Prio 3", + "extras": + { + "client::display": + { + "contentType": "text/html" + } + }, + "priority": 3 + }, + { + "message": "Some text", + "title": "Prio 4", + "extras": + { + "client::display": + { + "contentType": "text/html" + } + }, + "priority": 4 + }, + { + "message": "Some text", + "title": "Prio 15", + "extras": + { + "client::display": + { + "contentType": "text/html" + } + }, + "priority": 15 + }, + { + "message": "Some text", + "title": "Prio 21", + "extras": + { + "client::display": + { + "contentType": "text/html" + } + }, + "priority": 21 + }, + { + "message": "Some text", + "title": "Prio -5", + "extras": + { + "client::display": + { + "contentType": "text/html" + } + }, + "priority": -5 + }, + { + "message": "Some text", + "title": "No Prio", + "extras": + { + "client::display": + { + "contentType": "text/html" + } + }, + + }, +] + + +for data in priorities: + x = requests.post(url, data = json.dumps(data), headers=headers) + print(x.text) \ No newline at end of file diff --git a/execute_tests.sh b/execute_tests.sh index 50c5ff4..863ee90 100644 --- a/execute_tests.sh +++ b/execute_tests.sh @@ -1 +1 @@ -find . -name '*.go' -printf '%h\n' | sort -u | xargs -n1 -P1 go test -cover \ No newline at end of file +find . -name '*.go' -printf '%h\n' | sort -u | xargs -n1 -P1 go test -cover diff --git a/go.sum b/go.sum index b2e92a5..f61cc1d 100644 --- a/go.sum +++ b/go.sum @@ -109,6 +109,17 @@ golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c h1:VwygUrnw9jn88c4u8GD3rZQbq golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210608053332-aa57babbf139 h1:C+AwYEtBp/VQwoLntUmQ/yx3MS9vmZaKNdw5eOpoQe8= +golang.org/x/sys v0.0.0-20210608053332-aa57babbf139/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/internal/api/errors.go b/internal/api/errors.go deleted file mode 100644 index 64c744a..0000000 --- a/internal/api/errors.go +++ /dev/null @@ -1,5 +0,0 @@ -package api - -import "errors" - -var ErrorMessageNotFound = errors.New("message not found") diff --git a/internal/api/util.go b/internal/api/util.go index d045139..ff9e481 100644 --- a/internal/api/util.go +++ b/internal/api/util.go @@ -5,6 +5,7 @@ import ( "net/http" "github.com/pushbits/server/internal/authentication" + "github.com/pushbits/server/internal/pberrors" "github.com/gin-gonic/gin" ) @@ -13,7 +14,7 @@ func successOrAbort(ctx *gin.Context, code int, err error) bool { if err != nil { // If we know the error force error code switch err { - case ErrorMessageNotFound: + case pberrors.ErrorMessageNotFound: ctx.AbortWithError(http.StatusNotFound, err) default: ctx.AbortWithError(code, err) diff --git a/internal/dispatcher/notification.go b/internal/dispatcher/notification.go index 9fce7a0..67dcad9 100644 --- a/internal/dispatcher/notification.go +++ b/internal/dispatcher/notification.go @@ -8,8 +8,8 @@ import ( "github.com/gomarkdown/markdown" "github.com/matrix-org/gomatrix" - "github.com/pushbits/server/internal/api" "github.com/pushbits/server/internal/model" + "github.com/pushbits/server/internal/pberrors" ) // MessageFormat is a matrix message format @@ -77,7 +77,7 @@ func (d *Dispatcher) DeleteNotification(a *model.Application, n *model.DeleteNot if err != nil { log.Println(err) - return api.ErrorMessageNotFound + return pberrors.ErrorMessageNotFound } oldBody, oldFormattedBody, err = bodiesFromMessage(deleteMessage) @@ -182,7 +182,7 @@ func (d *Dispatcher) getMessage(a *model.Application, id string) (gomatrix.Event } start = messages.End } - return gomatrix.Event{}, api.ErrorMessageNotFound + return gomatrix.Event{}, pberrors.ErrorMessageNotFound } // Replaces the content of a matrix message @@ -254,19 +254,19 @@ func bodiesFromMessage(message gomatrix.Event) (body, formattedBody string, err body, ok := val.(string) if !ok { - return "", "", api.ErrorMessageNotFound + return "", "", pberrors.ErrorMessageNotFound } formattedBody = body } else { - return "", "", api.ErrorMessageNotFound + return "", "", pberrors.ErrorMessageNotFound } if val, ok := message.Content["formatted_body"]; ok { body, ok := val.(string) if !ok { - return "", "", api.ErrorMessageNotFound + return "", "", pberrors.ErrorMessageNotFound } formattedBody = body diff --git a/internal/pberrors/errors.go b/internal/pberrors/errors.go new file mode 100644 index 0000000..ff31d0c --- /dev/null +++ b/internal/pberrors/errors.go @@ -0,0 +1,6 @@ +package pberrors + +import "errors" + +// ErrorMessageNotFound indicates that a message does not exist +var ErrorMessageNotFound = errors.New("message not found") diff --git a/tests/mockups/dispatcher.go b/tests/mockups/dispatcher.go index 6a5b314..b273d39 100644 --- a/tests/mockups/dispatcher.go +++ b/tests/mockups/dispatcher.go @@ -7,11 +7,5 @@ import ( // GetMatrixDispatcher creates and returns a matrix dispatcher func GetMatrixDispatcher(homeserver, username, password string, confCrypto configuration.CryptoConfig) (*dispatcher.Dispatcher, error) { - db, err := GetEmptyDatabase(confCrypto) - - if err != nil { - return nil, err - } - - return dispatcher.Create(db, homeserver, username, password, configuration.Formatting{}) + return dispatcher.Create(homeserver, username, password, configuration.Formatting{}) }