-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from weni-ai/domains-restriction
Domains restriction
- Loading branch information
Showing
17 changed files
with
354 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
name: ci | ||
on: [push, pull_request] | ||
env: | ||
go-version: '1.17.x' | ||
go-version: '1.23' | ||
jobs: | ||
test: | ||
name: Test | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module github.com/ilhasoft/wwcs | ||
|
||
go 1.17 | ||
go 1.23 | ||
|
||
require ( | ||
github.com/adjust/rmq/v4 v4.0.1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<script> | ||
(function (d, s, u) { | ||
let h = d.getElementsByTagName(s)[0], k = d.createElement(s); | ||
k.onload = function () { | ||
let l = d.createElement(s); l.src = u; l.async = true; | ||
h.parentNode.insertBefore(l, k.nextSibling); | ||
}; | ||
k.async = true; k.src = 'https://storage.googleapis.com/push-webchat/wwc-latest.js'; | ||
h.parentNode.insertBefore(k, h); | ||
})(document, 'script', './script.js'); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package flows | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
type IClient interface { | ||
GetChannelAllowedDomains(string) ([]string, error) | ||
} | ||
|
||
type Client struct { | ||
BaseURL string `json:"base_url"` | ||
} | ||
|
||
func NewClient(baseURL string) *Client { | ||
return &Client{ | ||
BaseURL: baseURL, | ||
} | ||
} | ||
|
||
func (c *Client) GetChannelAllowedDomains(channelUUID string) ([]string, error) { | ||
url := fmt.Sprintf("%s/api/v2/internals/channel_allowed_domains?channel=%s", c.BaseURL, channelUUID) | ||
resp, err := http.Get(url) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return nil, fmt.Errorf("failed to get channel allowed domains, status code: %d", resp.StatusCode) | ||
} | ||
|
||
var domains []string | ||
err = json.NewDecoder(resp.Body).Decode(&domains) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return domains, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package flows | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetChannelAllowedDomains(t *testing.T) { | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
_, _ = w.Write([]byte("[\"domain1.com\", \"domain2.com\"]")) | ||
})) | ||
defer server.Close() | ||
|
||
client := Client{BaseURL: server.URL} | ||
|
||
domains, err := client.GetChannelAllowedDomains("09bf3dee-973e-43d3-8b94-441406c4a565") | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, 2, len(domains)) | ||
} | ||
|
||
func TestGetChannelAllowedDomainsStatus404(t *testing.T) { | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusNotFound) | ||
})) | ||
defer server.Close() | ||
|
||
client := Client{BaseURL: server.URL} | ||
|
||
_, err := client.GetChannelAllowedDomains("09bf3dee-973e-43d3-8b94-441406c4a565") | ||
|
||
assert.Equal(t, err.Error(), "failed to get channel allowed domains, status code: 404") | ||
} | ||
|
||
func TestGetChannelAllowedDomainsStatusWithNoDomain(t *testing.T) { | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
_, _ = w.Write([]byte("[]")) | ||
})) | ||
defer server.Close() | ||
|
||
client := Client{BaseURL: server.URL} | ||
|
||
domains, err := client.GetChannelAllowedDomains("09bf3dee-973e-43d3-8b94-441406c4a565") | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, 0, len(domains)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package memcache | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
type Cache[K comparable, V any] struct { | ||
items map[K]item[V] | ||
mu sync.Mutex | ||
} | ||
|
||
type item[V any] struct { | ||
value V | ||
expiry time.Time | ||
deleted bool | ||
} | ||
|
||
func New[K comparable, V any]() *Cache[K, V] { | ||
return &Cache[K, V]{ | ||
items: make(map[K]item[V]), | ||
} | ||
} | ||
|
||
func (c *Cache[K, V]) Set(key K, value V, ttl time.Duration) { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
c.items[key] = item[V]{ | ||
value: value, | ||
expiry: time.Now().Add(ttl), | ||
} | ||
} | ||
|
||
func (c *Cache[K, V]) Get(key K) (V, bool) { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
item, found := c.items[key] | ||
if !found || time.Now().After(item.expiry) || item.deleted { | ||
delete(c.items, key) | ||
return item.value, false | ||
} | ||
return item.value, true | ||
} | ||
|
||
func (c *Cache[K, V]) Remove(key K) { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
delete(c.items, key) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package memcache | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestCache(t *testing.T) { | ||
cacheDomains := New[string, []string]() | ||
chanUUID := "5f610454-98c1-4a54-9499-e2d2b9b68334" | ||
cacheDomains.Set(chanUUID, []string{"127.0.0.1", "localhost"}, time.Duration(time.Second*2)) | ||
|
||
chan1domains, ok := cacheDomains.Get(chanUUID) | ||
if !ok { | ||
t.Error("Expected channel UUID to be found") | ||
} | ||
if len(chan1domains) != 2 { | ||
t.Error("Expected 2 domains in cache, got", len(chan1domains)) | ||
} | ||
time.Sleep(3 * time.Second) | ||
|
||
_, ok = cacheDomains.Get(chanUUID) | ||
if ok { | ||
t.Error("Expected channel UUID not to be found") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.