Merge lp:~stolowski/unity-scopes-api/remote-scopes-partner-id-rtm into lp:unity-scopes-api/rtm-14.09

Proposed by Paweł Stołowski on 2014-12-16
Status: Merged
Approved by: Michi Henning on 2014-12-16
Approved revision: 253
Merged at revision: 255
Proposed branch: lp:~stolowski/unity-scopes-api/remote-scopes-partner-id-rtm
Merge into: lp:unity-scopes-api/rtm-14.09
Prerequisite: lp:~stolowski/unity-scopes-api/fix-lp-1401560-rtm
Diff against target: 236 lines (+78/-9)
7 files modified
include/unity/scopes/internal/smartscopes/SmartScopesClient.h (+3/-2)
src/scopes/internal/smartscopes/SmartScopesClient.cpp (+28/-2)
test/gtest/scopes/internal/smartscopes/RaiiServer.h (+2/-2)
test/gtest/scopes/internal/smartscopes/SmartScopesClient/CMakeLists.txt (+4/-0)
test/gtest/scopes/internal/smartscopes/SmartScopesClient/FakeSss.py (+9/-1)
test/gtest/scopes/internal/smartscopes/SmartScopesClient/SmartScopesClient_test.cpp (+31/-2)
test/gtest/scopes/internal/smartscopes/SmartScopesClient/partnerid (+1/-0)
To merge this branch: bzr merge lp:~stolowski/unity-scopes-api/remote-scopes-partner-id-rtm
Reviewer Review Type Date Requested Status
Michi Henning (community) Approve on 2014-12-16
Pete Woods 2014-12-16 Pending
Review via email: mp+244899@code.launchpad.net

This proposal supersedes a proposal from 2014-12-16.

Commit Message

Cherry-picked support for passing partner id from /custom/partner-id with remote-scopes requests maded by Smart Scopes Proxy.

Description of the Change

Cherry-picked support for passing partner id from /custom/partner-id with remote-scopes requests maded by Smart Scopes Proxy.

To post a comment you must log in.
Pete Woods (pete-woods) : Posted in a previous version of this proposal
review: Approve
Michi Henning (michihenning) wrote :

Cool!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'include/unity/scopes/internal/smartscopes/SmartScopesClient.h'
2--- include/unity/scopes/internal/smartscopes/SmartScopesClient.h 2014-09-22 13:43:00 +0000
3+++ include/unity/scopes/internal/smartscopes/SmartScopesClient.h 2014-12-16 18:36:42 +0000
4@@ -154,7 +154,8 @@
5
6 SmartScopesClient(HttpClientInterface::SPtr http_client,
7 JsonNodeInterface::SPtr json_node,
8- std::string const& url = ""); // detect url
9+ std::string const& url = "", // detect url
10+ std::string const& partner_id_path = "");
11
12 virtual ~SmartScopesClient();
13
14@@ -209,7 +210,6 @@
15
16 std::string stringify_settings(VariantMap const& settings);
17
18-private:
19 HttpClientInterface::SPtr http_client_;
20 JsonNodeInterface::SPtr json_node_;
21
22@@ -224,6 +224,7 @@
23 bool have_latest_cache_;
24
25 unsigned int query_counter_;
26+ std::string partner_file_;
27 };
28
29 } // namespace smartscopes
30
31=== modified file 'src/scopes/internal/smartscopes/SmartScopesClient.cpp'
32--- src/scopes/internal/smartscopes/SmartScopesClient.cpp 2014-12-16 18:36:42 +0000
33+++ src/scopes/internal/smartscopes/SmartScopesClient.cpp 2014-12-16 18:36:42 +0000
34@@ -23,6 +23,7 @@
35
36 #include <unity/scopes/ScopeExceptions.h>
37 #include <unity/UnityExceptions.h>
38+#include <unity/util/FileIO.h>
39
40 #include <algorithm>
41 #include <array>
42@@ -52,6 +53,7 @@
43
44 static const std::string c_scopes_cache_dir = homedir() + "/.cache/unity-scopes/";
45 static const std::string c_scopes_cache_filename = "remote-scopes.json";
46+static const std::string c_partner_id_file = "/custom/partner-id";
47
48 using namespace unity::scopes;
49 using namespace unity::scopes::internal::smartscopes;
50@@ -106,12 +108,19 @@
51
52 SmartScopesClient::SmartScopesClient(HttpClientInterface::SPtr http_client,
53 JsonNodeInterface::SPtr json_node,
54- std::string const& url)
55+ std::string const& url,
56+ std::string const& partner_id_path)
57 : http_client_(http_client)
58 , json_node_(json_node)
59 , have_latest_cache_(false)
60 , query_counter_(0)
61+ , partner_file_(partner_id_path)
62 {
63+ if (partner_file_.size() == 0)
64+ {
65+ partner_file_ = c_partner_id_file;
66+ }
67+
68 // initialise url_
69 reset_url(url);
70
71@@ -168,9 +177,26 @@
72
73 std::cout << "SmartScopesClient.get_remote_scopes(): GET " << remote_scopes_uri.str() << std::endl;
74
75+ HttpHeaders headers;
76+ try
77+ {
78+ auto partner_id = util::read_text_file(partner_file_);
79+ if (!partner_id.empty())
80+ {
81+ std::string const user_agent_hdr = "partner=" + http_client_->to_percent_encoding(partner_id);
82+ headers.push_back(std::make_pair("User-Agent", user_agent_hdr));
83+ std::cout << "SmartScopesClient.get_remote_scopes(): User agent: " << user_agent_hdr << std::endl;
84+ }
85+ }
86+ catch (std::exception const& e)
87+ {
88+ std::cerr << "SmartScopesClient.get_remote_scopes(): failed to read " << partner_file_ << ": " << e.what() << std::endl;
89+ }
90+
91 HttpResponseHandle::SPtr response = http_client_->get(remote_scopes_uri.str(), [&response_str](std::string const& replyLine) {
92 response_str += replyLine; // accumulate all reply lines
93- });
94+ }, headers);
95+
96 response->get();
97
98 std::cout << "SmartScopesClient.get_remote_scopes(): Remote scopes:" << std::endl << response_str << std::endl;
99
100=== modified file 'test/gtest/scopes/internal/smartscopes/RaiiServer.h'
101--- test/gtest/scopes/internal/smartscopes/RaiiServer.h 2014-08-04 06:31:27 +0000
102+++ test/gtest/scopes/internal/smartscopes/RaiiServer.h 2014-12-16 18:36:42 +0000
103@@ -42,7 +42,7 @@
104 class RaiiServer
105 {
106 public:
107- RaiiServer(std::string const& server_path)
108+ RaiiServer(std::string const& server_path, std::string const& arg = "")
109 {
110 int pipefd[2];
111 if (pipe(pipefd) < 0)
112@@ -62,7 +62,7 @@
113 throw unity::ResourceException("Write pipe duplication failed");
114 }
115
116- execl(server_path.c_str(), "", NULL);
117+ execl(server_path.c_str(), server_path.c_str(), arg.c_str(), NULL);
118 throw unity::ResourceException("Failed to execute fake server script");
119 default: // parent
120 close(pipefd[1]); // close write
121
122=== modified file 'test/gtest/scopes/internal/smartscopes/SmartScopesClient/CMakeLists.txt'
123--- test/gtest/scopes/internal/smartscopes/SmartScopesClient/CMakeLists.txt 2013-12-10 13:27:25 +0000
124+++ test/gtest/scopes/internal/smartscopes/SmartScopesClient/CMakeLists.txt 2014-12-16 18:36:42 +0000
125@@ -2,7 +2,11 @@
126 ${CMAKE_CURRENT_SOURCE_DIR}
127 )
128
129+configure_file(partnerid ${CMAKE_CURRENT_BINARY_DIR}/partnerid COPYONLY)
130+
131 add_definitions(-DFAKE_SSS_PATH="${CMAKE_CURRENT_SOURCE_DIR}/FakeSss.py")
132+add_definitions(-DFAKE_SSS_LOG="${CMAKE_CURRENT_BINARY_DIR}/fakesss.log")
133+add_definitions(-DPARTNER_FILE="${CMAKE_CURRENT_BINARY_DIR}/partnerid")
134
135 add_executable(
136 SmartScopesClient_test
137
138=== modified file 'test/gtest/scopes/internal/smartscopes/SmartScopesClient/FakeSss.py'
139--- test/gtest/scopes/internal/smartscopes/SmartScopesClient/FakeSss.py 2014-09-24 08:46:08 +0000
140+++ test/gtest/scopes/internal/smartscopes/SmartScopesClient/FakeSss.py 2014-12-16 18:36:42 +0000
141@@ -22,13 +22,18 @@
142 import sys
143
144 preview1_complete = False
145+outfile = ''
146
147 def response(environ, start_response):
148- global preview1_complete
149+ global preview1_complete, outfile
150 status = '200 OK'
151 response_headers = [('Content-Type', 'application/json')]
152 start_response(status, response_headers)
153
154+ if outfile != '':
155+ f = open(outfile, 'a')
156+ f.writelines(["%s : %s\n" % (environ['PATH_INFO'], environ['HTTP_USER_AGENT'])])
157+
158 if environ['PATH_INFO'] == '/remote-scopes' and (environ['QUERY_STRING'] == '' or environ['QUERY_STRING'] == 'locale=test_TEST'):
159 return [remote_scopes_response]
160
161@@ -59,6 +64,9 @@
162
163 return ''
164
165+
166+if len(sys.argv) > 1:
167+ outfile = sys.argv[1]
168 serving = False
169 port = 1024
170 while serving == False:
171
172=== modified file 'test/gtest/scopes/internal/smartscopes/SmartScopesClient/SmartScopesClient_test.cpp'
173--- test/gtest/scopes/internal/smartscopes/SmartScopesClient/SmartScopesClient_test.cpp 2014-09-24 08:46:08 +0000
174+++ test/gtest/scopes/internal/smartscopes/SmartScopesClient/SmartScopesClient_test.cpp 2014-12-16 18:36:42 +0000
175@@ -22,6 +22,9 @@
176 #include <unity/scopes/internal/smartscopes/SmartScopesClient.h>
177
178 #include <unity/UnityExceptions.h>
179+#include <unity/util/FileIO.h>
180+#include <boost/filesystem/operations.hpp>
181+#include <fstream>
182
183 #include "../RaiiServer.h"
184
185@@ -44,10 +47,26 @@
186 SmartScopesClientTest()
187 : http_client_(new HttpClientQt(20000)),
188 json_node_(new JsonCppNode()),
189- server_(FAKE_SSS_PATH)
190+ server_(FAKE_SSS_PATH, FAKE_SSS_LOG)
191 {
192+ boost::filesystem::remove(FAKE_SSS_LOG);
193 sss_url_ = "http://127.0.0.1:" + std::to_string(server_.port_);
194- ssc_ = std::make_shared<SmartScopesClient>(http_client_, json_node_, sss_url_);
195+ ssc_ = std::make_shared<SmartScopesClient>(http_client_, json_node_, sss_url_, PARTNER_FILE);
196+ }
197+
198+ bool grep_string(std::string const &s)
199+ {
200+ std::stringstream str(unity::util::read_text_file(FAKE_SSS_LOG));
201+ while (str)
202+ {
203+ char tmp[1024];
204+ str.getline(tmp, 1024);
205+ if (strstr(tmp, s.c_str()))
206+ {
207+ return true;
208+ }
209+ }
210+ return false;
211 }
212
213 protected:
214@@ -113,6 +132,16 @@
215 "\"defaultValue\":23},\"type\":\"number\"},{\"displayName\":\"Enabled\",\"id\":"
216 "\"enabled\",\"parameters\":{\"defaultValue\":true},\"type\":\"boolean\"}]\n",
217 *scopes[2].settings);
218+
219+ EXPECT_TRUE(grep_string("/remote-scopes : partner=Partner%20String"));
220+}
221+
222+TEST_F(SmartScopesClientTest, remote_scopes_no_partner)
223+{
224+ std::vector<RemoteScope> scopes;
225+ auto ssc_no_partner_ = std::make_shared<SmartScopesClient>(http_client_, json_node_, sss_url_, "/this/file/doesnt/exist");
226+ EXPECT_TRUE(ssc_no_partner_->get_remote_scopes(scopes, "", false));
227+ EXPECT_FALSE(grep_string("/remote-scopes : partner"));
228 }
229
230 TEST_F(SmartScopesClientTest, search)
231
232=== added file 'test/gtest/scopes/internal/smartscopes/SmartScopesClient/partnerid'
233--- test/gtest/scopes/internal/smartscopes/SmartScopesClient/partnerid 1970-01-01 00:00:00 +0000
234+++ test/gtest/scopes/internal/smartscopes/SmartScopesClient/partnerid 2014-12-16 18:36:42 +0000
235@@ -0,0 +1,1 @@
236+Partner String

Subscribers

People subscribed via source and target branches

to all changes: