Skip to content

Commit

Permalink
Merge branch 'master' into jwt-add-role-support
Browse files Browse the repository at this point in the history
  • Loading branch information
atrauzzi committed Mar 21, 2020
2 parents dfcd01b + 3248ebc commit 4964881
Show file tree
Hide file tree
Showing 9 changed files with 267 additions and 8 deletions.
6 changes: 3 additions & 3 deletions test/elixir/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ X means done, - means partially
- [X] Port etags_head.js
- [ ] ~~Port etags_views.js~~ (skipped in js test suite)
- [X] Port form_submit.js
- [ ] Port http.js
- [X] Port http.js
- [X] Port invalid_docids.js
- [ ] Port jsonp.js
- [X] Port jsonp.js
- [X] Port large_docs.js
- [ ] Port list_views.js
- [X] Port lorem_b64.txt
- [X] Port lorem.txt
- [X] Port lots_of_docs.js
- [ ] Port method_override.js
- [X] Port method_override.js
- [X] Port multiple_rows.js
- [X] Port proxyauth.js
- [ ] Port purge.js
Expand Down
4 changes: 2 additions & 2 deletions test/elixir/lib/couch/db_test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,8 @@ defmodule Couch.DBTest do
Enum.each(setting.nodes, fn node_value ->
node = elem(node_value, 0)
value = elem(node_value, 1)

if value == ~s(""\\n) do
if value == ~s(""\\n) or value == "" or value == nil do
resp =
Couch.delete(
"/_node/#{node}/_config/#{setting.section}/#{setting.key}",
Expand Down
81 changes: 81 additions & 0 deletions test/elixir/test/http_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
defmodule HttpTest do
use CouchTestCase

@moduletag :http

@tag :with_db
test "location header", context do
db_name = context[:db_name]
resp = Couch.put("/#{db_name}/test", body: %{})
db_url = Couch.process_url("/" <> db_name)
assert resp.headers.hdrs["location"] == db_url <> "/test"
end

@tag :with_db
test "location header should include X-Forwarded-Host", context do
db_name = context[:db_name]

resp =
Couch.put("/#{db_name}/test2",
body: %{},
headers: ["X-Forwarded-Host": "mysite.com"]
)

assert resp.headers.hdrs["location"] == "http:https://mysite.com/#{db_name}/test2"
end

@tag :with_db
test "location header should include custom header", context do
db_name = context[:db_name]

server_config = [
%{
:section => "httpd",
:key => "x_forwarded_host",
:value => "X-Host"
}
]

run_on_modified_server(server_config, fn ->
resp =
Couch.put("/#{db_name}/test3",
body: %{},
headers: ["X-Host": "mysite2.com"]
)

assert resp.headers.hdrs["location"] == "http:https://mysite2.com/#{db_name}/test3"
end)
end

@tag :with_db
test "COUCHDB-708: newlines document names", context do
db_name = context[:db_name]

resp =
Couch.put("/#{db_name}/docid%0A/attachment.txt",
body: %{},
headers: ["Content-Type": "text/plain;charset=utf-8"]
)

db_url = Couch.process_url("/" <> db_name)
assert resp.headers.hdrs["location"] == db_url <> "/docid%0A/attachment.txt"

resp =
Couch.put("/#{db_name}/docidtest%0A",
body: %{},
headers: ["Content-Type": "text/plain;charset=utf-8"]
)

db_url = Couch.process_url("/" <> db_name)
assert resp.headers.hdrs["location"] == db_url <> "/docidtest%0A"

resp =
Couch.post("/#{db_name}/",
body: %{_id: "docidtestpost%0A"},
headers: ["Content-Type": "application/json"]
)

db_url = Couch.process_url("/" <> db_name)
assert resp.headers.hdrs["location"] == db_url <> "/docidtestpost%250A"
end
end
116 changes: 116 additions & 0 deletions test/elixir/test/jsonp_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
defmodule JsonpTest do
use CouchTestCase

@moduletag :jsonp

@tag :with_db
test "jsonp not configured callbacks", context do
db_name = context[:db_name]
{:ok, _} = create_doc(db_name, %{_id: "0", a: 0, b: 0})

resp = Couch.get("/#{db_name}/0?callback=jsonp_no_chunk")
assert resp.status_code == 200
assert resp.headers.hdrs["content-type"] == "application/json"
end

@tag :with_db
test "jsonp unchunked callbacks", context do
db_name = context[:db_name]

server_config = [
%{
:section => "httpd",
:key => "allow_jsonp",
:value => "true"
}
]

{:ok, create_resp} = create_doc(db_name, %{_id: "0", a: 0, b: 0})

run_on_modified_server(server_config, fn ->
resp = Couch.get("/#{db_name}/0?callback=jsonp_no_chunk")

assert resp.status_code == 200
assert resp.headers.hdrs["content-type"] == "application/javascript"

{callback_fun, callback_param} = parse_callback(resp.body)

assert callback_fun == "jsonp_no_chunk"
assert create_resp.body["id"] == callback_param["_id"]
assert create_resp.body["rev"] == callback_param["_rev"]

resp = Couch.get("/#{db_name}/0?callback=jsonp_no_chunk\"")
assert resp.status_code == 400
end)
end

@tag :with_db
test "jsonp chunked callbacks", context do
db_name = context[:db_name]

server_config = [
%{
:section => "httpd",
:key => "allow_jsonp",
:value => "true"
}
]

design_doc = %{
_id: "_design/test",
language: "javascript",
views: %{
all_docs: %{map: "function(doc) {if(doc.a) emit(null, doc.a);}"}
}
}

{:ok, _} = create_doc(db_name, design_doc)
{:ok, _} = create_doc(db_name, %{_id: "0", a: 0, b: 0})
{:ok, _} = create_doc(db_name, %{_id: "1", a: 1, b: 1})

run_on_modified_server(server_config, fn ->
resp = Couch.get("/#{db_name}/_design/test/_view/all_docs?callback=jsonp_chunk")
assert resp.status_code == 200
assert resp.headers.hdrs["content-type"] == "application/javascript"

{callback_fun, callback_param} = parse_callback(resp.body)

assert callback_fun == "jsonp_chunk"
assert callback_param["total_rows"] == 1

resp = Couch.get("/#{db_name}/_design/test/_view/all_docs?callback=jsonp_chunk'")
assert resp.status_code == 400

resp = Couch.get("/#{db_name}/_changes?callback=jsonp_chunk")
assert resp.status_code == 200
assert resp.headers.hdrs["content-type"] == "application/javascript"

{callback_fun, callback_param} = parse_callback(resp.body)
assert callback_fun == "jsonp_chunk"
assert length(callback_param["results"]) == 3

end)
end

defp parse_callback(msg) do
captures = Regex.scan(~r/\/\* CouchDB \*\/(\w+)\((.*)\)/s, msg)

callback_fun =
captures
|> Enum.map(fn p -> Enum.at(p, 1) end)
|> Enum.at(0)

param =
captures
|> Enum.map(fn p -> Enum.at(p, 2) end)
|> Enum.filter(fn p -> String.trim(p) != "" end)
|> Enum.map(fn p ->
p
|> IO.iodata_to_binary()
|> :jiffy.decode([:return_maps])
end)
|> Enum.at(0)

{callback_fun, param}
end
end
55 changes: 55 additions & 0 deletions test/elixir/test/method_override_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
defmodule MethodOverrideTest do
use CouchTestCase

@moduletag :http

@moduledoc """
Allow broken HTTP clients to fake a full method vocabulary with an
X-HTTP-METHOD-OVERRIDE header
"""

@tag :with_db
test "method override PUT", context do
db_name = context[:db_name]

resp =
Couch.post("/#{db_name}/fnord",
body: %{bob: "connie"},
headers: ["X-HTTP-Method-Override": "PUT"]
)

assert resp.status_code == 201

resp = Couch.get("/#{db_name}/fnord")
assert resp.body["bob"] == "connie"
end

@tag :with_db
test "method override DELETE", context do
db_name = context[:db_name]
{:ok, resp} = create_doc(db_name, %{_id: "fnord", bob: "connie"})

resp =
Couch.post("/#{db_name}/fnord?rev=#{resp.body["rev"]}",
headers: ["X-HTTP-Method-Override": "DELETE"]
)

assert resp.status_code == 200

resp = Couch.get("/#{db_name}/fnord")
assert resp.status_code == 404
end

@tag :with_db
test "Method Override is ignored when original Method isn't POST", context do
db_name = context[:db_name]

resp =
Couch.get("/#{db_name}/fnord2",
body: %{bob: "connie"},
headers: ["X-HTTP-Method-Override": "PUT"]
)

assert resp.status_code == 404
end
end
6 changes: 4 additions & 2 deletions test/javascript/tests/changes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

couchTests.elixir = true;

function jsonp(obj) {
return console.log('done in test/elixir/test/changes_test.exs and changes_async_test.exs');
T(jsonp_flag == 0);
T(obj.results.length == 1 && obj.last_seq == 1, "jsonp");
jsonp_flag = 1;
}

couchTests.changes = function(debug) {
return console.log('done in test/elixir/test/changes_test.exs and changes_async_test.exs');

var db;
if (debug) debugger;

Expand Down
3 changes: 2 additions & 1 deletion test/javascript/tests/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

couchTests.elixir = true;
couchTests.http = function(debug) {
return console.log('done in test/elixir/test/http_test.exs');
var db_name = get_random_db_name();
var db = new CouchDB(db_name, {"X-Couch-Full-Commit":"false"});

Expand Down
2 changes: 2 additions & 0 deletions test/javascript/tests/jsonp.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
couchTests.elixir = true;

// Verify callbacks ran
var jsonp_flag = 0;
Expand All @@ -28,6 +29,7 @@ function jsonp_chunk(doc) {

// Do some jsonp tests.
couchTests.jsonp = function(debug) {
return console.log('done in test/elixir/test/jsonp_test.exs');
var db_name = get_random_db_name();
var db = new CouchDB(db_name, {"X-Couch-Full-Commit":"false"});
db.createDb();
Expand Down
2 changes: 2 additions & 0 deletions test/javascript/tests/method_override.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
// the License.

// Allow broken HTTP clients to fake a full method vocabulary with an X-HTTP-METHOD-OVERRIDE header
couchTests.elixir = true;
couchTests.method_override = function(debug) {
return console.log('done in test/elixir/test/method_override_test.exs');
var result = JSON.parse(CouchDB.request("GET", "/").responseText);
T(result.couchdb == "Welcome");

Expand Down

0 comments on commit 4964881

Please sign in to comment.