Skip to content

Commit c11fba1

Browse files
authored
Merge pull request #633 from marle3003/develop
Develop
2 parents dbc6218 + cdaa8ad commit c11fba1

31 files changed

+960
-418
lines changed

acceptance/mail_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (suite *MailSuite) TestSendMail() {
9494
smtptest.WithSubject("Test Mail"),
9595
smtptest.WithRootCa(ca),
9696
)
97-
require.EqualError(suite.T(), err, "550 [5 1 0] rule allowSender: sender [email protected] does not match allow rule: .*@foo.bar")
97+
require.EqualError(suite.T(), err, "550 [5 1 0] Rule allowSender: Sender [email protected] does not match allow rule: .*@foo.bar")
9898

9999
//from := "[email protected]"
100100
//password := "super_secret_password"

acceptance/petstore_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ func (suite *PetStoreSuite) TestKafka3_Consume() {
327327
func (suite *PetStoreSuite) TestSearch_Paging() {
328328
time.Sleep(3 * time.Second)
329329

330-
try.GetRequest(suite.T(), fmt.Sprintf("http://127.0.0.1:%v/api/search/query?q=api:%%22Swagger%%20Petstore%%22", suite.cfg.Api.Port),
330+
try.GetRequest(suite.T(), fmt.Sprintf("http://127.0.0.1:%v/api/search/query?q=api:%%22Swagger%%20Petstore%%22%%20type:http", suite.cfg.Api.Port),
331331
nil,
332332
try.HasStatusCode(http.StatusOK),
333333
try.AssertBody(func(t *testing.T, body string) {
@@ -347,7 +347,7 @@ func (suite *PetStoreSuite) TestSearch_Paging() {
347347
}),
348348
)
349349

350-
try.GetRequest(suite.T(), fmt.Sprintf("http://127.0.0.1:%v/api/search/query?q=api:%%22Swagger%%20Petstore%%22&index=1", suite.cfg.Api.Port),
350+
try.GetRequest(suite.T(), fmt.Sprintf("http://127.0.0.1:%v/api/search/query?q=api:%%22Swagger%%20Petstore%%22%%20type:http&index=1", suite.cfg.Api.Port),
351351
nil,
352352
try.HasStatusCode(http.StatusOK),
353353
try.AssertBody(func(t *testing.T, body string) {

api/handler_mail.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ func (h *handler) handleMailService(w http.ResponseWriter, r *http.Request) {
213213
if s.Store.Settings != nil {
214214
result.Settings.MaxRecipients = s.Store.Settings.MaxRecipients
215215
result.Settings.AutoCreateMailbox = s.Store.Settings.AutoCreateMailbox
216+
} else {
217+
result.Settings.AutoCreateMailbox = true
216218
}
217219

218220
for mName, m := range s.Store.Mailboxes {

api/handler_mail_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestHandler_Smtp(t *testing.T) {
4646
responseBody: `[{"name":"foo","description":"bar","version":"2.1","type":"mail"}]`,
4747
},
4848
{
49-
name: "/api/services/mail",
49+
name: "get /api/services/mail",
5050
app: func() *runtime.App {
5151
app := runtime.New(&static.Config{})
5252
app.Mail.Set("foo", &runtime.MailInfo{
@@ -57,7 +57,7 @@ func TestHandler_Smtp(t *testing.T) {
5757
},
5858
requestUrl: "http://foo.api/api/services/mail/foo",
5959
contentType: "application/json",
60-
responseBody: `{"name":"foo","settings":{"maxRecipients":0,"autoCreateMailbox":false}}`,
60+
responseBody: `{"name":"foo","settings":{"maxRecipients":0,"autoCreateMailbox":true}}`,
6161
},
6262
{
6363
name: "get smtp service",
@@ -77,7 +77,7 @@ func TestHandler_Smtp(t *testing.T) {
7777
},
7878
requestUrl: "http://foo.api/api/services/mail/foo",
7979
contentType: "application/json",
80-
responseBody: `{"name":"foo","mailboxes":[{"name":"[email protected]","username":"alice","password":"foo","numMessages":0}],"configs":[{"id":"64613435-3062-6462-3033-316532633233","url":"file://foo.yml","provider":"test","time":"2023-12-27T13:01:30Z"}],"settings":{"maxRecipients":0,"autoCreateMailbox":false}}`,
80+
responseBody: `{"name":"foo","mailboxes":[{"name":"[email protected]","username":"alice","password":"foo","numMessages":0}],"configs":[{"id":"64613435-3062-6462-3033-316532633233","url":"file://foo.yml","provider":"test","time":"2023-12-27T13:01:30Z"}],"settings":{"maxRecipients":0,"autoCreateMailbox":true}}`,
8181
},
8282
{
8383
name: "get smtp service with mailbox",
@@ -101,7 +101,7 @@ func TestHandler_Smtp(t *testing.T) {
101101
},
102102
requestUrl: "http://foo.api/api/services/mail/foo",
103103
contentType: "application/json",
104-
responseBody: `{"name":"foo","mailboxes":[{"name":"[email protected]","username":"alice","password":"foo","numMessages":0}],"settings":{"maxRecipients":0,"autoCreateMailbox":false}}`,
104+
responseBody: `{"name":"foo","mailboxes":[{"name":"[email protected]","username":"alice","password":"foo","numMessages":0}],"settings":{"maxRecipients":0,"autoCreateMailbox":true}}`,
105105
},
106106
{
107107
name: "get smtp service with rules",
@@ -125,7 +125,7 @@ func TestHandler_Smtp(t *testing.T) {
125125
},
126126
requestUrl: "http://foo.api/api/services/mail/foo",
127127
contentType: "application/json",
128-
responseBody: `{"name":"foo","rules":[{"name":"foo","sender":"[email protected]","recipient":"[email protected]","subject":"foo","body":"bar","action":"deny"}],"settings":{"maxRecipients":0,"autoCreateMailbox":false}}`,
128+
responseBody: `{"name":"foo","rules":[{"name":"foo","sender":"[email protected]","recipient":"[email protected]","subject":"foo","body":"bar","action":"deny"}],"settings":{"maxRecipients":0,"autoCreateMailbox":true}}`,
129129
},
130130
{
131131
name: "get smtp mailboxes",

api/handler_search.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package api
22

33
import (
4-
"github.com/pkg/errors"
54
"mokapi/runtime/search"
65
"net/http"
76
"net/url"
@@ -12,6 +11,12 @@ const queryText = "q"
1211
const searchLimit = "limit"
1312
const searchIndex = "index"
1413

14+
var searchFacetExcludeQueryParams = map[string]bool{
15+
queryText: true,
16+
searchLimit: true,
17+
searchIndex: true,
18+
}
19+
1520
func (h *handler) getSearchResults(w http.ResponseWriter, r *http.Request) {
1621
w.Header().Set("Content-Type", "application/json")
1722

@@ -26,13 +31,11 @@ func (h *handler) getSearchResults(w http.ResponseWriter, r *http.Request) {
2631
return
2732
}
2833

34+
sr.Facets = getFacets(r.URL.Query())
35+
2936
results, err := h.app.Search(sr)
3037
if err != nil {
31-
if errors.Is(err, &search.ErrNotEnabled{}) {
32-
writeError(w, err, http.StatusBadRequest)
33-
} else {
34-
writeError(w, err, http.StatusInternalServerError)
35-
}
38+
writeError(w, err, http.StatusBadRequest)
3639
} else {
3740
writeJsonBody(w, results)
3841
}
@@ -47,3 +50,13 @@ func getQueryParamInsensitive(values url.Values, key string) string {
4750
}
4851
return ""
4952
}
53+
54+
func getFacets(query url.Values) map[string]string {
55+
facets := make(map[string]string)
56+
for key, values := range query {
57+
if !searchFacetExcludeQueryParams[key] {
58+
facets[key] = values[0]
59+
}
60+
}
61+
return facets
62+
}

api/handler_search_test.go

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
package api
22

33
import (
4+
"encoding/json"
5+
"github.com/stretchr/testify/require"
46
"mokapi/config/dynamic"
7+
"mokapi/config/dynamic/asyncApi/asyncapitest"
58
"mokapi/config/dynamic/dynamictest"
69
"mokapi/config/static"
7-
"mokapi/providers/openapi"
10+
"mokapi/engine/enginetest"
811
"mokapi/providers/openapi/openapitest"
912
"mokapi/runtime"
13+
"mokapi/runtime/search"
1014
"mokapi/try"
1115
"net/http"
1216
"testing"
1317
)
1418

1519
func TestHandler_SearchQuery(t *testing.T) {
16-
toConfig := func(c *openapi.Config) *dynamic.Config {
20+
toConfig := func(c any) *dynamic.Config {
1721
cfg := &dynamic.Config{
1822
Info: dynamictest.NewConfigInfo(),
1923
Data: c,
@@ -34,7 +38,7 @@ func TestHandler_SearchQuery(t *testing.T) {
3438
response: []try.ResponseCondition{
3539
try.HasStatusCode(200),
3640
try.HasHeader("Content-Type", "application/json"),
37-
try.HasBody(`{"results":[{"type":"HTTP","title":"foo","params":{"service":"foo","type":"http"}}],"total":1}`),
41+
try.HasBody(`{"results":[{"type":"HTTP","title":"foo","params":{"service":"foo","type":"http"}}],"facets":{"type":[{"value":"HTTP","count":1}]},"total":1}`),
3842
},
3943
app: func() *runtime.App {
4044
app := runtime.New(&static.Config{Api: static.Api{Search: static.Search{
@@ -53,7 +57,7 @@ func TestHandler_SearchQuery(t *testing.T) {
5357
response: []try.ResponseCondition{
5458
try.HasStatusCode(200),
5559
try.HasHeader("Content-Type", "application/json"),
56-
try.HasBody(`{"results":[{"type":"HTTP","title":"foo","fragments":["\u003cmark\u003efoo\u003c/mark\u003e"],"params":{"service":"foo","type":"http"}}],"total":1}`),
60+
try.HasBody(`{"results":[{"type":"HTTP","title":"foo","fragments":["\u003cmark\u003efoo\u003c/mark\u003e"],"params":{"service":"foo","type":"http"}}],"facets":{"type":[{"value":"HTTP","count":1}]},"total":1}`),
5761
},
5862
app: func() *runtime.App {
5963
app := runtime.New(&static.Config{Api: static.Api{Search: static.Search{
@@ -72,7 +76,7 @@ func TestHandler_SearchQuery(t *testing.T) {
7276
response: []try.ResponseCondition{
7377
try.HasStatusCode(200),
7478
try.HasHeader("Content-Type", "application/json"),
75-
try.HasBody(`{"results":[{"type":"HTTP","title":"foo","fragments":["\u003cmark\u003efoo\u003c/mark\u003e"],"params":{"service":"foo","type":"http"}}],"total":1}`),
79+
try.HasBody(`{"results":[{"type":"HTTP","title":"foo","fragments":["\u003cmark\u003efoo\u003c/mark\u003e"],"params":{"service":"foo","type":"http"}}],"facets":{"type":[{"value":"HTTP","count":1}]},"total":1}`),
7680
},
7781
app: func() *runtime.App {
7882
app := runtime.New(&static.Config{Api: static.Api{Search: static.Search{
@@ -105,6 +109,36 @@ func TestHandler_SearchQuery(t *testing.T) {
105109
cfg = openapitest.NewConfig("3.0", openapitest.WithInfo("bar", "", ""))
106110
app.AddHttp(toConfig(cfg))
107111

112+
return app
113+
},
114+
},
115+
{
116+
name: "using facet type=Kafka should filter out HTTP (case insensitive)",
117+
requestUrl: "/api/search/query?q=foo&type=Kafka",
118+
response: []try.ResponseCondition{
119+
try.HasStatusCode(200),
120+
try.HasHeader("Content-Type", "application/json"),
121+
try.AssertBody(func(t *testing.T, body string) {
122+
var result search.Result
123+
err := json.Unmarshal([]byte(body), &result)
124+
require.NoError(t, err)
125+
require.Len(t, result.Facets, 1)
126+
require.Equal(t, []search.FacetValue{{Value: "HTTP", Count: 1}, {Value: "Kafka", Count: 1}}, result.Facets["type"])
127+
require.Len(t, result.Results, 1)
128+
require.Equal(t, result.Results[0].Type, "Kafka")
129+
}),
130+
},
131+
app: func() *runtime.App {
132+
app := runtime.New(&static.Config{Api: static.Api{Search: static.Search{
133+
Enabled: true,
134+
}}})
135+
136+
h := openapitest.NewConfig("3.0", openapitest.WithInfo("foo", "", ""))
137+
app.AddHttp(toConfig(h))
138+
k := asyncapitest.NewConfig(asyncapitest.WithInfo("foo", "", ""))
139+
_, err := app.Kafka.Add(toConfig(k), enginetest.NewEngine())
140+
require.NoError(t, err)
141+
108142
return app
109143
},
110144
},

config/dynamic/mail/convert.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ func (c *Config) Convert() *mail.Config {
3737
}
3838
}
3939

40+
result.Settings = &mail.Settings{
41+
AutoCreateMailbox: c.AutoCreateMailbox,
42+
MaxRecipients: c.MaxRecipients,
43+
}
44+
4045
for _, mb := range c.Mailboxes {
4146
m := &mail.MailboxConfig{
4247
Username: mb.Username,

config/dynamic/mail/convert_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,30 @@ func TestConvert(t *testing.T) {
138138
require.Len(t, mb.Folders["folder"].Folders, 0)
139139
},
140140
},
141+
{
142+
name: "default settings",
143+
cfg: &Config{},
144+
test: func(t *testing.T, c *mail.Config) {
145+
require.NotNil(t, c)
146+
require.Equal(t, &mail.Settings{
147+
MaxRecipients: 0,
148+
AutoCreateMailbox: false,
149+
MaxInboxMails: 0,
150+
}, c.Settings)
151+
},
152+
},
153+
{
154+
name: "set settings",
155+
cfg: &Config{
156+
AutoCreateMailbox: false,
157+
MaxRecipients: 10,
158+
},
159+
test: func(t *testing.T, c *mail.Config) {
160+
require.NotNil(t, c)
161+
require.Equal(t, false, c.Settings.AutoCreateMailbox)
162+
require.Equal(t, 10, c.Settings.MaxRecipients)
163+
},
164+
},
141165
{
142166
name: "rules",
143167
cfg: &Config{

examples/ldap/users.ldif

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ dn: uid=awilliams,dc=mokapi,dc=io
44
cn: Alice Williams
55
uid: awilliams
66
userPassword: foo123
7+
objectSid:: AQUAAAAAAAUVAAAAF8sUcR3r8QcekDXQw9wAAA==
78

89
dn: uid=bmiller,dc=mokapi,dc=io
910
cn: Bob Miller

examples/mail/smtp.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
smtp: '1.0'
22
info:
3-
title: Mokapi's Mail Server
3+
title: Mokapi's Mail Server Legacy
44
servers:
55
- url: smtps://127.0.0.1:2525
66
mailboxes:

0 commit comments

Comments
 (0)