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: Superseded
Proposed branch: lp:~stolowski/unity-scopes-api/remote-scopes-partner-id-rtm
Merge into: lp:unity-scopes-api/rtm-14.09
Diff against target: 238 lines (+79/-10)
7 files modified
include/unity/scopes/internal/smartscopes/SmartScopesClient.h (+3/-2)
src/scopes/internal/smartscopes/SmartScopesClient.cpp (+29/-3)
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
Pete Woods (community) 2014-12-16 Approve on 2014-12-16
Review via email: mp+244837@code.launchpad.net

This proposal has been superseded by 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) :
review: Approve
253. By Paweł Stołowski on 2014-12-16

Merged fix-lp-1401560-rtm

Unmerged revisions

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:34:21 +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-11-04 14:37:36 +0000
33+++ src/scopes/internal/smartscopes/SmartScopesClient.cpp 2014-12-16 18:34:21 +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,10 +177,27 @@
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- response->wait();
95+ }, headers);
96+
97+ response->get();
98
99 std::cout << "SmartScopesClient.get_remote_scopes(): Remote scopes:" << std::endl << response_str << std::endl;
100 }
101
102=== modified file 'test/gtest/scopes/internal/smartscopes/RaiiServer.h'
103--- test/gtest/scopes/internal/smartscopes/RaiiServer.h 2014-08-04 06:31:27 +0000
104+++ test/gtest/scopes/internal/smartscopes/RaiiServer.h 2014-12-16 18:34:21 +0000
105@@ -42,7 +42,7 @@
106 class RaiiServer
107 {
108 public:
109- RaiiServer(std::string const& server_path)
110+ RaiiServer(std::string const& server_path, std::string const& arg = "")
111 {
112 int pipefd[2];
113 if (pipe(pipefd) < 0)
114@@ -62,7 +62,7 @@
115 throw unity::ResourceException("Write pipe duplication failed");
116 }
117
118- execl(server_path.c_str(), "", NULL);
119+ execl(server_path.c_str(), server_path.c_str(), arg.c_str(), NULL);
120 throw unity::ResourceException("Failed to execute fake server script");
121 default: // parent
122 close(pipefd[1]); // close write
123
124=== modified file 'test/gtest/scopes/internal/smartscopes/SmartScopesClient/CMakeLists.txt'
125--- test/gtest/scopes/internal/smartscopes/SmartScopesClient/CMakeLists.txt 2013-12-10 13:27:25 +0000
126+++ test/gtest/scopes/internal/smartscopes/SmartScopesClient/CMakeLists.txt 2014-12-16 18:34:21 +0000
127@@ -2,7 +2,11 @@
128 ${CMAKE_CURRENT_SOURCE_DIR}
129 )
130
131+configure_file(partnerid ${CMAKE_CURRENT_BINARY_DIR}/partnerid COPYONLY)
132+
133 add_definitions(-DFAKE_SSS_PATH="${CMAKE_CURRENT_SOURCE_DIR}/FakeSss.py")
134+add_definitions(-DFAKE_SSS_LOG="${CMAKE_CURRENT_BINARY_DIR}/fakesss.log")
135+add_definitions(-DPARTNER_FILE="${CMAKE_CURRENT_BINARY_DIR}/partnerid")
136
137 add_executable(
138 SmartScopesClient_test
139
140=== modified file 'test/gtest/scopes/internal/smartscopes/SmartScopesClient/FakeSss.py'
141--- test/gtest/scopes/internal/smartscopes/SmartScopesClient/FakeSss.py 2014-09-24 08:46:08 +0000
142+++ test/gtest/scopes/internal/smartscopes/SmartScopesClient/FakeSss.py 2014-12-16 18:34:21 +0000
143@@ -22,13 +22,18 @@
144 import sys
145
146 preview1_complete = False
147+outfile = ''
148
149 def response(environ, start_response):
150- global preview1_complete
151+ global preview1_complete, outfile
152 status = '200 OK'
153 response_headers = [('Content-Type', 'application/json')]
154 start_response(status, response_headers)
155
156+ if outfile != '':
157+ f = open(outfile, 'a')
158+ f.writelines(["%s : %s\n" % (environ['PATH_INFO'], environ['HTTP_USER_AGENT'])])
159+
160 if environ['PATH_INFO'] == '/remote-scopes' and (environ['QUERY_STRING'] == '' or environ['QUERY_STRING'] == 'locale=test_TEST'):
161 return [remote_scopes_response]
162
163@@ -59,6 +64,9 @@
164
165 return ''
166
167+
168+if len(sys.argv) > 1:
169+ outfile = sys.argv[1]
170 serving = False
171 port = 1024
172 while serving == False:
173
174=== modified file 'test/gtest/scopes/internal/smartscopes/SmartScopesClient/SmartScopesClient_test.cpp'
175--- test/gtest/scopes/internal/smartscopes/SmartScopesClient/SmartScopesClient_test.cpp 2014-09-24 08:46:08 +0000
176+++ test/gtest/scopes/internal/smartscopes/SmartScopesClient/SmartScopesClient_test.cpp 2014-12-16 18:34:21 +0000
177@@ -22,6 +22,9 @@
178 #include <unity/scopes/internal/smartscopes/SmartScopesClient.h>
179
180 #include <unity/UnityExceptions.h>
181+#include <unity/util/FileIO.h>
182+#include <boost/filesystem/operations.hpp>
183+#include <fstream>
184
185 #include "../RaiiServer.h"
186
187@@ -44,10 +47,26 @@
188 SmartScopesClientTest()
189 : http_client_(new HttpClientQt(20000)),
190 json_node_(new JsonCppNode()),
191- server_(FAKE_SSS_PATH)
192+ server_(FAKE_SSS_PATH, FAKE_SSS_LOG)
193 {
194+ boost::filesystem::remove(FAKE_SSS_LOG);
195 sss_url_ = "http://127.0.0.1:" + std::to_string(server_.port_);
196- ssc_ = std::make_shared<SmartScopesClient>(http_client_, json_node_, sss_url_);
197+ ssc_ = std::make_shared<SmartScopesClient>(http_client_, json_node_, sss_url_, PARTNER_FILE);
198+ }
199+
200+ bool grep_string(std::string const &s)
201+ {
202+ std::stringstream str(unity::util::read_text_file(FAKE_SSS_LOG));
203+ while (str)
204+ {
205+ char tmp[1024];
206+ str.getline(tmp, 1024);
207+ if (strstr(tmp, s.c_str()))
208+ {
209+ return true;
210+ }
211+ }
212+ return false;
213 }
214
215 protected:
216@@ -113,6 +132,16 @@
217 "\"defaultValue\":23},\"type\":\"number\"},{\"displayName\":\"Enabled\",\"id\":"
218 "\"enabled\",\"parameters\":{\"defaultValue\":true},\"type\":\"boolean\"}]\n",
219 *scopes[2].settings);
220+
221+ EXPECT_TRUE(grep_string("/remote-scopes : partner=Partner%20String"));
222+}
223+
224+TEST_F(SmartScopesClientTest, remote_scopes_no_partner)
225+{
226+ std::vector<RemoteScope> scopes;
227+ auto ssc_no_partner_ = std::make_shared<SmartScopesClient>(http_client_, json_node_, sss_url_, "/this/file/doesnt/exist");
228+ EXPECT_TRUE(ssc_no_partner_->get_remote_scopes(scopes, "", false));
229+ EXPECT_FALSE(grep_string("/remote-scopes : partner"));
230 }
231
232 TEST_F(SmartScopesClientTest, search)
233
234=== added file 'test/gtest/scopes/internal/smartscopes/SmartScopesClient/partnerid'
235--- test/gtest/scopes/internal/smartscopes/SmartScopesClient/partnerid 1970-01-01 00:00:00 +0000
236+++ test/gtest/scopes/internal/smartscopes/SmartScopesClient/partnerid 2014-12-16 18:34:21 +0000
237@@ -0,0 +1,1 @@
238+Partner String

Subscribers

People subscribed via source and target branches

to all changes: