1. photobucket
  2. api-java

Commits

Eric Schlenz  committed 07e9601

DE218 - Ads seen for pro users bug. Making changes to the way information is supplied to the DirectLoginStrategy, UserInfoStrategy and UserDemoStrategies.

  • Participants
  • Parent commits ae4f881
  • Branches release-1.4.2

Comments (0)

Files changed (11)

File service/src/com/photobucket/api/service/DirectLoginStrategy.java

View file
     private User user;
     private String uniqueDeviceId;
     
-	public DirectLoginStrategy(User user) {
-		this.user = user;
+	public DirectLoginStrategy(String username, String password) {
+		this.user = new User();
+		this.user.setUsername(username);
+		this.user.setPassword(password);
 	}
 	
 	protected Logger getLogger() {

File service/src/com/photobucket/api/service/UserInfoStrategy.java

View file
 	private static final long serialVersionUID = 8788054802899980815L;
 	private static final Logger logger = LoggerFactory.getLogger(UserInfoStrategy.class);
 	
-	private User user;
-	private String username;
+	private User asUser; // User making request.
+	private User forUser; // User to get info for.
+
+	/**
+	 * 
+	 * @param forUsername Username to get info for.
+	 */
+	public UserInfoStrategy(String forUsername) {
+        this(null, forUsername);
+    }
 	
-	private User userInfo;
-	
-	public UserInfoStrategy(User user) {
-		this.user = user;
+	/**
+	 * 
+	 * @param forUser User to get info for.
+	 */
+	public UserInfoStrategy(User forUser) {
+	    this(null, forUser);
 	}
 	
-	public UserInfoStrategy(User user, String username) {
-		this.user = user;
-		this.username = username;
+	/**
+	 * 
+	 * @param asUser User making request.
+	 * @param forUser User to get info for.
+	 */
+	public UserInfoStrategy(User asUser, User forUser) {
+		this.asUser = asUser;
+		this.forUser = forUser;
 	}
 	
-	public UserInfoStrategy(String username) {
-	    this.username = username;
+	/**
+	 * 
+	 * @param asUser User making request.
+	 * @param forUsername Username to get info for.
+	 */
+	public UserInfoStrategy(User asUser, String forUsername) {
+		this.asUser = asUser;
+		this.forUser = new User();
+		this.forUser.setUsername(forUsername);
 	}
-
+	
 	public User getUserInfo() {
-		return userInfo;
+		return forUser;
 	}
 
 	@Override
 		ToStringBuilder builder = new ToStringBuilder(this, stringStyle);
 		
 		// Who is asking?
-		if (user != null) {
-		    builder.append(user.getUsername());
+		if (asUser != null) {
+		    builder.append(asUser.getUsername());
 		}
 		
 		// Who are they asking about?
-		if (username == null) {
-		    builder.append(user.getUsername());
-		} else {
-		    builder.append(username);
-		}
-		
-		return md5(builder.toString());
+	    builder.append(forUser.getUsername());
+
+	    return md5(builder.toString());
 	}
 
 	@Override
 		StringBuilder sb = new StringBuilder();
 		sb.append("/user");
 		
-		if (username != null) {
+		if (forUser.getUsername() != null) {
 			sb.append('/');
-			sb.append(username);
+			sb.append(forUser.getUsername());
 		}
 		
 		return sb.toString();
 	@Override
 	protected void parseContent(Object contentObject) throws JSONException {
 		JSONObject content = (JSONObject)contentObject;
-		userInfo = User.fromJson(content, user);
+		User.fromJson(content, asUser, forUser);
 	}
 	
 	@Override
 	protected void preExecute() {
 	    super.preExecute();
 
-	    if (user != null) {
-            api.setSubdomain(user.getSubdomain());
-            api.setOauthToken(user.getOauthToken());
-            api.setOauthTokenSecret(user.getOauthTokenSecret());
+	    if (asUser != null) {
+            api.setSubdomain(asUser.getSubdomain());
+            api.setOauthToken(asUser.getOauthToken());
+            api.setOauthTokenSecret(asUser.getOauthTokenSecret());
 	    }
 	}
 }

File service/src/com/photobucket/api/service/model/User.java

View file
         return !OfferType.TERMS_OF_SERVICE.getValue().equals(offer);
     }
     
-    public static User fromJson(JSONObject content, User apiUser) throws JSONException {
-        User user = new User();
-        user.setAlbumUrl(content.getString("album_url"));
-        user.setLikesBy(content.optInt("likes_by")); // Won't always be available.
-        user.setPremium(content.optInt("premium") != 0);
-        user.setProfilePicUrl(content.getString("profile_pic"));
-        user.setProfilePicUrlLarge(content.optString("profile_pic_large"));
+    public static User fromJson(JSONObject content, User asUser) throws JSONException {
+        return fromJson(content, asUser, null);
+    }
+    
+    public static User fromJson(JSONObject content, User asUser, User forUser) throws JSONException {
+        if (forUser == null) {
+            forUser = new User();
+        }
+        
+        forUser.setAlbumUrl(content.getString("album_url"));
+        forUser.setLikesBy(content.optInt("likes_by")); // Won't always be available.
+        forUser.setPremium(content.optInt("premium") != 0);
+        forUser.setProfilePicUrl(content.getString("profile_pic"));
+        forUser.setProfilePicUrlLarge(content.optString("profile_pic_large"));
         if (content.has("total_pictures")) {
-            user.setTotalPictures(Integer.valueOf(content.getString("total_pictures")));
+            forUser.setTotalPictures(Integer.valueOf(content.getString("total_pictures")));
         }
-        user.setUsername(content.getString("username"));
+        forUser.setUsername(content.getString("username"));
         
         // Was this a request for the authenticated user's info?
-        if (apiUser != null && apiUser.getUsername().equals(user.getUsername())) {
+        if (asUser != null && asUser.getUsername().equals(forUser.getUsername())) {
             // Yes, so read additional info.
-            user.setMegabytesAllowed(content.optInt("megabytes_allowed"));
-            user.setMegabytesUsed(content.optInt("megabytes_used"));
-            user.setPreferredPictureSize(content.optString("preferred_picture_size"));
-            user.setPublic(content.optInt("public") != 0);
-            user.setUploadEmail(content.optString("upload_email"));
-        } else if (apiUser != null && content.has("isFollowing")) {
-            user.setFollowing(JSONUtil.GetBooleanValue(content, "isFollowing"));
+            forUser.setMegabytesAllowed(content.optInt("megabytes_allowed"));
+            forUser.setMegabytesUsed(content.optInt("megabytes_used"));
+            forUser.setPreferredPictureSize(content.optString("preferred_picture_size"));
+            forUser.setPublic(content.optInt("public") != 0);
+            forUser.setUploadEmail(content.optString("upload_email"));
+        } else if (asUser != null && content.has("isFollowing")) {
+            forUser.setFollowing(JSONUtil.GetBooleanValue(content, "isFollowing"));
         }
         
-        return user;
+        return forUser;
     }
 }

File service/test/com/photobucket/api/service/AlbumCreateTest.java

View file
 		String consumerSecretKey = ResourceBundle.getBundle("consumer").getString("oauth.consumer.key");
 		String subdomain = ResourceBundle.getBundle("consumer").getString("subdomain");
 		PhotobucketService service = new PhotobucketService(consumerKey, consumerSecretKey, subdomain);
-		User user = new User();
-		user.setUsername("pbapi");
-		user.setPassword("abc123");
+		User user = null;
 		Album album = new Album();
 		album.setPath("/api-test");	
 		try {
-			DirectLoginStrategy strategy = new DirectLoginStrategy(user);
+			DirectLoginStrategy strategy = new DirectLoginStrategy("pbapi", "abc123");
 			service.execute(strategy);
 		    user = strategy.getUser();
 

File service/test/com/photobucket/api/service/AlbumReadTest.java

View file
 		String consumerSecretKey = ResourceBundle.getBundle("consumer").getString("oauth.consumer.key");
 		String subdomain = ResourceBundle.getBundle("consumer").getString("subdomain");
 		PhotobucketService service = new PhotobucketService(consumerKey, consumerSecretKey, subdomain);
-		User user = new User();
-		user.setUsername("pbapi");
-		user.setPassword("abc123");
+		User user = null;
 		Album album = new Album();
 		album.setPath("/");	
 		try {
-			DirectLoginStrategy strategy = new DirectLoginStrategy(user);
+			DirectLoginStrategy strategy = new DirectLoginStrategy("pbapi", "abc123");
 			service.execute(strategy);
 		    user = strategy.getUser();
 

File service/test/com/photobucket/api/service/DirectLoginStrategyTest.java

View file
 public class DirectLoginStrategyTest extends ApiTestCase {
     @Test
     public void testValidLogin() throws Exception {
-        User tempUser = new User();
-        tempUser.setUsername(bundle.getString("username"));
-        tempUser.setPassword(bundle.getString("password"));
-        DirectLoginStrategy strategy = new DirectLoginStrategy(tempUser);
+        DirectLoginStrategy strategy = new DirectLoginStrategy(
+            bundle.getString("username"), bundle.getString("password"));
         service.execute(strategy);
         
-        tempUser = strategy.getUser();
+        User tempUser = strategy.getUser();
         
         Assert.assertNotNull(tempUser);
         Assert.assertNotNull(tempUser.getOauthToken());
     
     @Test
     public void testValidLoginWithDeviceId() throws Exception {
-        User tempUser = new User();
-        tempUser.setUsername(bundle.getString("username"));
-        tempUser.setPassword(bundle.getString("password"));
-        DirectLoginStrategy strategy = new DirectLoginStrategy(tempUser);
+        DirectLoginStrategy strategy = new DirectLoginStrategy(
+            bundle.getString("username"), bundle.getString("password"));
         strategy.setUniqueDeviceId("WWWW-XXXX-YYYY-ZZZZ-ApiTest");
         service.execute(strategy);
         
-        tempUser = strategy.getUser();
+        User tempUser = strategy.getUser();
         
         Assert.assertNotNull(tempUser);
         Assert.assertNotNull(tempUser.getOauthToken());

File service/test/com/photobucket/api/service/MediaDeleteTest.java

View file
 		String subdomain = ResourceBundle.getBundle("consumer").getString("subdomain");
 		PhotobucketService service = new PhotobucketService(consumerKey, consumerSecretKey, subdomain);
 
-		User user = new User();
-        user.setUsername("pbapi");
-        user.setPassword("abc123");
+		User user = null;
         
         Media media = new Media();
         media.setUrl("http://i244.photobucket.com/albums/gg17/pbapi/api-test/wintersnowangel1.jpg");
         
         try {
             
-            DirectLoginStrategy strategy = new DirectLoginStrategy(user);
+            DirectLoginStrategy strategy = new DirectLoginStrategy("pbapi", "abc123");
             service.execute(strategy);
             user = strategy.getUser();
 

File service/test/com/photobucket/api/service/MediaDescriptionTest.java

View file
 		String subdomain = ResourceBundle.getBundle("consumer").getString("subdomain");
 		PhotobucketService service = new PhotobucketService(consumerKey, consumerSecretKey, subdomain);
 
-        User user = new User();
-        user.setUsername("pbapi");
-        user.setPassword("abc123");
+        User user = null;
         
         Media media = new Media();
         media.setUrl("http://i244.photobucket.com/albums/gg17/pbapi/api-test/CoorsField-3.jpg");
         
         try {
             
-            DirectLoginStrategy strategy = new DirectLoginStrategy(user);
+            DirectLoginStrategy strategy = new DirectLoginStrategy("pbapi", "abc123");
             service.execute(strategy);
             user = strategy.getUser();
 
 		String subdomain = ResourceBundle.getBundle("consumer").getString("subdomain");
 		PhotobucketService service = new PhotobucketService(consumerKey, consumerSecretKey, subdomain);
 
-		User user = new User();
-        user.setUsername("pbapi");
-        user.setPassword("abc123");
+		User user = null;
+		MediaSetDescriptionStrategy descStrategy = null;
         
         Media media = new Media();
         media.setUrl("http://i244.photobucket.com/albums/gg17/pbapi/api-test/CoorsField-3.jpg_nosuchfile");
         media.setDescription("no file here");
-        MediaSetDescriptionStrategy descStrategy = new MediaSetDescriptionStrategy(user, media);
-        
         try {
             
-            DirectLoginStrategy strategy = new DirectLoginStrategy(user);
+            DirectLoginStrategy strategy = new DirectLoginStrategy("pbapi", "abc123");
             service.execute(strategy);
             user = strategy.getUser();
-            
+
+            descStrategy = new MediaSetDescriptionStrategy(user, media);
             service.execute(descStrategy);
             System.out.println("Response: "+descStrategy.getApiResponse().getResponseString());
             fail("Long title failed to throw exception");

File service/test/com/photobucket/api/service/MediaTitleTest.java

View file
 		String subdomain = ResourceBundle.getBundle("consumer").getString("subdomain");
 		PhotobucketService service = new PhotobucketService(consumerKey, consumerSecretKey, subdomain);
 
-        User user = new User();
-        user.setUsername("pbapi");
-        user.setPassword("abc123");
+        User user = null;
         
         Media media = new Media();
         media.setUrl("http://i244.photobucket.com/albums/gg17/pbapi/api-test/CoorsField-3.jpg");
         
         try {
             
-            DirectLoginStrategy strategy = new DirectLoginStrategy(user);
+            DirectLoginStrategy strategy = new DirectLoginStrategy("pbapi", "abc123");
             service.execute(strategy);
             user = strategy.getUser();
 
 		String subdomain = ResourceBundle.getBundle("consumer").getString("subdomain");
 		PhotobucketService service = new PhotobucketService(consumerKey, consumerSecretKey, subdomain);
 
-        User user = new User();
-        user.setUsername("pbapi");
-        user.setPassword("abc123");
+        User user = null;
+        MediaSetTitleStrategy titleStrategy = null;
         
         Media media = new Media();
         media.setUrl("http://i244.photobucket.com/albums/gg17/pbapi/api-test/CoorsField-3.jpg_nosuchfile");
         media.setTitle("no file here");
-        MediaSetTitleStrategy titleStrategy = new MediaSetTitleStrategy(user, media);
         
         try {
             
-            DirectLoginStrategy strategy = new DirectLoginStrategy(user);
+            DirectLoginStrategy strategy = new DirectLoginStrategy("pbapi", "abc123");
             service.execute(strategy);
             user = strategy.getUser();
-            
+
+            titleStrategy = new MediaSetTitleStrategy(user, media);
             service.execute(titleStrategy);
             System.out.println("Response: "+titleStrategy.getApiResponse().getResponseString());
             fail("Long title failed to throw exception");

File service/test/com/photobucket/api/service/SiteLoginTest.java

View file
 		String subdomain = ResourceBundle.getBundle("consumer").getString("subdomain");
 		PhotobucketService service = new PhotobucketService(consumerKey, consumerSecretKey, subdomain);
 
-		User user = new User();
-		user.setUsername("pbapi");
-		user.setPassword("abc123");
+		User user = null;
 		
 		try {
-		    DirectLoginStrategy strategy = new DirectLoginStrategy(user);
+		    DirectLoginStrategy strategy = new DirectLoginStrategy("pbapi", "abc123");
 			service.execute(strategy);
 		    user = strategy.getUser();
 		   

File service/test/com/photobucket/api/test/SecureTestCase.java

View file
 	}
 	
 	protected void login() throws Exception {
-		User tempUser = new User();
-		tempUser.setUsername(bundle.getString("username"));
-		tempUser.setPassword(bundle.getString("password"));
-		DirectLoginStrategy strategy = new DirectLoginStrategy(tempUser);
+		DirectLoginStrategy strategy = new DirectLoginStrategy(
+		    bundle.getString("username"), bundle.getString("password"));
 		service.execute(strategy);
 		
-		user = strategy.getUser();
+		User user = strategy.getUser();
 		
 		// Override the subdomain if we're not targeting production
 		String subdomain = bundle.getString("subdomain");